문제

I have legacy database which entities were mapped using composite key. I converted the tables into entities with Eclipse JPA Tools but the received entities are not working. During Tomcat startup I receive the exception: referencedColumnNames(PETROL_STATION_ID, PROVIDER_ID) of xxx.FuelCardEntity.petrolStationInfo referencing xxx.PetrolStationInfoEntity not mapped to a single property

Composite key class:

@Embeddable
public class PetrolStationInfoEntityPK implements Serializable {
private static final long serialVersionUID = 1L;

@Column(name="PETROL_STATION_ID", insertable=false, updatable=false)
private long petrolStationId;

@Column(name="PROVIDER_ID", insertable=false, updatable=false)
private long providerId;

@Column(name = "\"VERSION\"")
private long version;

// hashCode and equals method
}

PetrolStationInfoEntity class:

@Entity
@Table(name="PETROL_STATION_INFO")
@NamedQuery(name="PetrolStationInfoEntity.findAll", query="SELECT p FROM    PetrolStationInfoEntity p")
public class PetrolStationInfoEntity implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private PetrolStationInfoEntityPK id;
}

The FuelCardEntity class holding the relation:

public class FuelCardEntity implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private FuelCardEntityPK id;

// bi-directional many-to-one association to PetrolStationInfoEntity
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
        @JoinColumn(name = "INFO_PETROL_STATION_ID", referencedColumnName = "PETROL_STATION_ID", nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "INFO_PROVIDER_ID", referencedColumnName = "PROVIDER_ID", nullable = false, insertable = false, updatable = false) })
private PetrolStationInfoEntity petrolStationInfo;

The only advice I found already was to use @JoinColumns but as you can see, it is already there and it still does not work. Any idea how to fix the problem without changing the database schema? The application is written with Spring 3 + Hibernate 4.

Thanks in advance!

도움이 되었습니까?

해결책

Your problem is the key! You have 3 attributes in the key obejct - JPA assumes that you need all 3 columns for a unique identification. Therefore it throws an error when you try to use @JoinColumns with only the first 2 columns, cause he expects 3. When you add the third column VERSION to @JoinColumns it should work!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top