Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

hibernate - One to many association - Join tables with non primary key column in JPA

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

SecondaryTable JoinColumn cannot reference a non primary key.

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the JPA 2.0 specs paragraph 11.1.21 JoinColumn annotaion on page 379

Support for referenced columns that are not primary key columns of the referenced table is optional. Applications that use such mappings will not be portable.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...