2012-07-27 97 views
0

我一直在嘗試幾天,以獲得複合鍵來看看網上的許多例子,但仍然失去了一些東西,因爲它不工作。使用下面的代碼發佈我目前遇到以下錯誤:無法確定類型:java.util.Set,在表:USER_PROFILE,爲列:[org.hibernate.mapping.Column(userProfileDataFilter)]。JPA複合鍵與額外的列映射

如果我從UserProfile對象註釋掉UserProfileDataFilter,所有內容都會保存並刪除,並且運行平穩。我已經把變量的註釋移到了getters上,但並不幸運。所以,我不知道還有什麼需要做的,這就是爲什麼我在這裏尋求幫助。

任何指導通過這個邪惡的註解迷宮是非常讚賞。

USER PROFILE CLASS:

@Entity 
@Table(name="USER_PROFILE") 
public class UserProfile extends UserProfileAbstract implements Serializable { 

private static final long serialVersionUID = CommonDefines.SERIALVERSIONUID; 

public Company company; 
private Set<UserProfileDataFilter> userProfileDataFilter = new HashSet<UserProfileDataFilter>(0); 

public UserProfile(){super();} 
public UserProfile(String ploginName, String ppassword, Date ppasswordExprDt, String pfirstName, String plastName, 
     String pemail, UUID pcompanyId, Long pstatusId, UUID pcrtdId, Date pcrtdDt, UUID pmodId, Date pmodDt){ 

    super(ploginName, ppassword, ppasswordExprDt, pfirstName, plastName, 
      pemail, pcompanyId, pstatusId, pcrtdId, pcrtdDt, pmodId, pmodDt); 
} 

@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name="COMPANY_ID",insertable=false,updatable=false,nullable=false) 
public Company getCompany() { 
    return company; 
} 
public void setCompany(Company company) { 
    this.company = company; 
} 

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.userProfile", cascade=CascadeType.ALL) 
public Set<UserProfileDataFilter> getUserProfileDataFilter() { 
    return userProfileDataFilter; 
} 
public void setUserProfileDataFilter(
     Set<UserProfileDataFilter> userProfileDataFilter) { 
    this.userProfileDataFilter = userProfileDataFilter; 
} 

用戶簡檔數據的過濾器類(COMPOSITE KEY):

@Entity 
@Table(name="USER_PROFILE_DATA_FILTER") 
@AssociationOverrides({ 
@AssociationOverride(name="pk.userProfile", [email protected](name="USER_PROFILE_ID")), 
@AssociationOverride(name="pk.dataFilter", [email protected](name="DATA_FILTER_ID")) }) 
public class UserProfileDataFilter implements Serializable { 

private static final long serialVersionUID = CommonDefines.SERIALVERSIONUID; 
private UserProfileDataFilterPK pk = new UserProfileDataFilterPK(); 
private UUID crtdId; 
private Date crtdDt; 
private UUID modId; 
private Date modDt; 

@EmbeddedId 
public UserProfileDataFilterPK getPk() { 
    return pk; 
} 

public void setPk(UserProfileDataFilterPK pk) { 
    this.pk = pk; 
} 

@Transient 
public UserProfile getUserProfile() { 
    return getPk().getUserProfile(); 
} 

public void setUserProfile(UserProfile userProfile) { 
    getPk().setUserProfile(userProfile); 
} 

@Transient 
public DataFilter getDataFilter() { 
    return getPk().getDataFilter(); 
} 

public void setDataFilter(DataFilter dataFilter) { 
    getPk().setDataFilter(dataFilter); 
} 


@Column(name="CRTD_ID", nullable=false) 
public UUID getCrtdId() { 
    return crtdId; 
} 

public void setCrtdId(UUID crtdId) { 
    this.crtdId = crtdId; 
} 

@Column(name="CRTD_DT", nullable=false) 
@Temporal(TemporalType.TIMESTAMP) 
public Date getCrtdDt() { 
    return crtdDt; 
} 

public void setCrtdDt(Date crtdDt) { 
    this.crtdDt = crtdDt; 
} 

@Column(name="MOD_ID", nullable=false) 
public UUID getModId() { 
    return modId; 
} 

public void setModId(UUID modId) { 
    this.modId = modId; 
} 

@Column(name="MOD_DT", nullable=false) 
@Temporal(TemporalType.TIMESTAMP) 
public Date getModDt() { 
    return modDt; 
} 

public void setModDt(Date modDt) { 
    this.modDt = modDt; 
} 
} 

用戶簡檔數據FILTER PK(類別用於定義鍵)

@Embeddable 
public class UserProfileDataFilterPK implements Serializable { 


private static final long serialVersionUID = CommonDefines.SERIALVERSIONUID; 
private UserProfile userProfile; 
private DataFilter dataFilter; 

@ManyToOne 
public UserProfile getUserProfile() { 
    return userProfile; 
} 
public void setUserProfile(UserProfile userProfile) { 
    this.userProfile = userProfile; 
} 

@ManyToOne 
public DataFilter getDataFilter() { 
    return dataFilter; 
} 
public void setDataFilter(DataFilter dataFilter) { 
    this.dataFilter = dataFilter; 
} 

public boolean equals(Object o) { 
if (this == o) return true; 
if (o == null || getClass() != o.getClass()) return false; 

UserProfileDataFilterPK that = (UserProfileDataFilterPK) o; 

if (userProfile != null ? !userProfile.equals(that.userProfile) : that.userProfile != null) return false; 
if (dataFilter != null ? !dataFilter.equals(that.dataFilter) : that.dataFilter != null) 
    return false; 

return true; 
} 

public int hashCode() { 
    int result; 
    result = (userProfile != null ? userProfile.hashCode() : 0); 
    result = 31 * result + (dataFilter != null ? dataFilter.hashCode() : 0); 
    return result; 
} 
} 

數據過濾器類:

@Entity 
@Table(name="DATA_FILTER") 
public class DataFilter extends DataFilterAbstract { 

private static final long serialVersionUID = CommonDefines.SERIALVERSIONUID; 

@Transient 
protected boolean selected; 

@OneToMany(mappedBy="dataFilterId") 
public Set<DataFilterDetail> dataFilterDetails; 

public DataFilter(){super();} 
public DataFilter(boolean pselected, String pname, String pdescription, boolean pActive, UUID pcrtdId, Date pcrtdDt, UUID pmodId, Date pmodDt){ 
    super(pname,pdescription,pActive, pcrtdId, pcrtdDt, pmodId, pmodDt); 
    selected = pselected; 
} 

public boolean isSelected() { 
    return selected; 
} 
public void setSelected(boolean selected) { 
    this.selected = selected; 
} 
public Set<DataFilterDetail> getDataFilterDetails() { 
    return dataFilterDetails; 
} 
public void setDataFilterDetails(Set<DataFilterDetail> dataFilterDetails) { 
    this.dataFilterDetails = dataFilterDetails; 
} 

}

回答

0

有時候你穿上領域註釋,有時在accessores(吸氣)。始終將它們放在給定層次的類中的相同位置。

IIRC,Hibernate只考慮與@Id(或@EmbeddedId)註釋位於相同位置的註釋。因此,如果@Id位於吸氣器上,並且字段上有@OneToMany,則@OneToMany註釋將被忽略。

+0

我一直在做這個,就像你在上面的例子中看到的那樣,但是你的回答讓我想起了更多。它讓我想到了我在大多數對象上使用的抽象類來定義表結構。在那裏,我對所有導致「不一致」問題的變量聲明都加上了註釋。謝謝 !! – MikeR 2012-07-27 15:32:01