问题描述
我正在研究遗留系统,需要从数据库中读取一些信息。以下是表格关系:
供应商(vendorId - pk,vendorEid,name)
VendorContactBridge(bridgeId -pk,vendorEid,contactEid)
联系人(contactId -pk,contactEid,电话)
vendorEid和contactEid不是表格的主键,而是用作Join表格VendorContactBridge中的连接列。
供应商实体 -
@Entity
@Table (name =Vendor)
public class供应商实现Serializable {
@Id
@Column(name =VENDORID)
private BigDecimal vendorId;
@Column(name =VENDOREID)
private BigDecimal vendorEid;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name =VENDORCONTACTBRIDGE,
joinColumns = {@ JoinColumn(name =VENDOREID,referencedColumnName =VENDOREID )},
inverseJoinColumns = {@ JoinColumn(name =CONTACTEID,referencedColumnName =CONTACTEID)})
private Set< Contact> vendorContact;
code
联系实体 - $ /
@Entity
@Table(name =CONTACT)
public class Contact实现Serializable {
@Id
@Column(name =CONTACTID)
private BigDecimal contactId;
@Column(name =CONTATEID)
私人BigDecimal contactEId;
@ManyToOne
@JoinTable(name =VENDORCONTACTBRIDGE,
joinColumns = {@ JoinColumn(name =CONTACTEID,referencedColumnName =CONTATEID)},
reverseJoinColumns = {@ JoinColumn(name =VENDOREID,referencedColumnName =VENDOREID)})
私人供应商供应商;
}
在运行查询时,获取下面的异常
我删除了Eager获取我在供应商实体给出的,我没有得到任何例外,但它不加载集合。关联有什么问题?
解决方案根据第11.1.21节JoinColumn注释(第379页)
看起来Hibernate选择不实现这个可选部分。其他实现可能。我在EclipseLink上试过了,但那个也不行(它验证失败)。
我看到两个解决方法。一种是调整你的模式以使用主键,这从数据库设计理论的角度来看是正确的。然而,这可能不是一个选项,因为其他软件取决于这个模式,这留下了选项二。通过不改变JPA中的关系来解决它,只需使用eid并自己检索相关对象。
I'm working on legacy system, need to read some of the info from database. Below are the table relationship
Vendor (vendorId - pk, vendorEid, name)
VendorContactBridge (bridgeId -pk, vendorEid, contactEid)
Contact (contactId -pk, contactEid, phone)
vendorEid and contactEid are not the primary key of the table but used as join column in Join table VendorContactBridge.
Vendor Entity -
@Entity
@Table(name="Vendor")
public class Vendor implements Serializable{
@Id
@Column(name="VENDORID")
private BigDecimal vendorId;
@Column(name="VENDOREID")
private BigDecimal vendorEid;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name="VENDORCONTACTBRIDGE",
joinColumns={@JoinColumn(name="VENDOREID", referencedColumnName="VENDOREID")},
inverseJoinColumns={@JoinColumn(name="CONTACTEID", referencedColumnName="CONTACTEID")})
private Set<Contact> vendorContact;
}
Contact Entity -
@Entity
@Table(name="CONTACT")
public class Contact implements Serializable{
@Id
@Column(name="CONTACTID")
private BigDecimal contactId;
@Column(name="CONTATEID")
private BigDecimal contactEId;
@ManyToOne
@JoinTable(name="VENDORCONTACTBRIDGE",
joinColumns={@JoinColumn(name="CONTACTEID", referencedColumnName="CONTATEID")},
inverseJoinColumns={@JoinColumn(name="VENDOREID", referencedColumnName="VENDOREID")})
private Vendor vendor;
}
while running the query, getting below exception
I removed the Eager fetch which i have given in Vendor entity, i dont get any exception but it doesn't load the collection. What's wrong with association ?
解决方案 According to the JPA 2.0 specs paragraph 11.1.21 JoinColumn annotaion on page 379
It seems Hibernate choose not to implement this optional part. Other implementations might. I tried it on EclipseLink but that one does not work either (it fails validation).
I see two work arounds. One is to adjust your schema to use the primary keys which would be the right thing from a database design theory perspective. However that might not be an option because of other software depending on this schema which leaves option two. Work around it by NOT moddeling the relationship in JPA just use the eid's and retrieve the relevant objects yourself.
这篇关于一对多关联 - 在JPA中使用非主键列联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!