带有复合键的ManyToOne关系 [英] ManyToOne relation with composite key

查看:66
本文介绍了带有复合键的ManyToOne关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有旧版数据库,其中使用组合键映射了哪些实体.我使用Eclipse JPA工具将表转换为实体,但是收到的实体不起作用.在Tomcat启动期间,我收到以下异常: referencedColumnNames(PETROL_STATION_ID, PROVIDER_ID) of xxx.FuelCardEntity.petrolStationInfo referencing xxx.PetrolStationInfoEntity not mapped to a single property

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

复合键类:

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

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

拥有该关系的FuelCardEntity类:

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;

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

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.

提前谢谢!

推荐答案

您的问题是关键!您在键对象中具有3个属性-JPA假定您需要所有3列才能唯一标识.因此,当您尝试仅对前两列使用@JoinColumns时,它将引发错误,因为他期望3.将第三列VERSION添加到@JoinColumns时,它将起作用!

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!

这篇关于带有复合键的ManyToOne关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆