Hibernate OnetoOne mapping with two lookup values
I have a class which has two lookup values (one for name and one for type)
public class someAttr {
private Long someAttrId;
private Long projectId;
private Lookup typeLookupValue;
private Lookup nameLookupValue;
getters & setter...
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "type_lk_id", referencedColumnName = "lk_id", insertable = false, updatable = false)
public LookupValue getTypeLookupValue() {
return typeLookupValue;
}
public void setTypeLookupValue(Lookup typeLookupValue) {
this.typeLookupValue= typeLookupValue;
}
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "name_lk_id", referencedColumnName = "lk_id", insertable = false, updatable = false)
public LookupValue getNameLookupValue() {
return nameLookupValue;
}
public void setNameLookupValue(开发者_如何转开发Lookup nameLookupValue) {
this.nameLookupValue= nameLookupValue;
}
The problem is that the same name_lk_id is shared between different someAttr object. Every time I run it I get: "org.hibernate.HibernateException: More than one row with the given identifier was found".
Can I get nameLookupValue which belongs to a specific someAttr with a projectId instead of getting multiple Lookup object with the same "lk_id"??
One someAttr instance is associated to two Lookup value instances. This is not a One-To-One relationship. You need to use One-to-Many relationship for this. You can have helper methods inside your class to retrieve the approrpiate Lookup instance from the List/Set.
精彩评论