ManyToOne与复合键的关系

时间:2014-02-11 09:22:42

标签: hibernate jpa-2.0

我有遗留数据库,哪些实体使用复合键映射。我使用Eclipse JPA Tools将表转换为实体,但接收的实体不起作用。在Tomcat启动期间,我收到异常: referencedColumnNames(PETROL_STATION_ID, PROVIDER_ID) of xxx.FuelCardEntity.petrolStationInfo referencing xxx.PetrolStationInfoEntity not mapped to a single property

复合键类:

@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类:

@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;
}

持有关系的FuelCardEntity类:

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;

我发现的唯一建议是使用@JoinColumns,但正如您所看到的,它已经存在,但仍然无效。知道如何在不更改数据库架构的情况下解决问题吗? 该应用程序是用Spring 3 + Hibernate 4编写的。

提前致谢!

1 个答案:

答案 0 :(得分:1)

你的问题是关键!密钥obejct中有3个属性 - JPA假定您需要所有3列才能获得唯一标识。因此,当您尝试仅使用前2列的@JoinColumns时会抛出错误,因为他期望3.当您将第三列VERSION添加到@JoinColumns时,它应该可以工作!