![](/img/trans.png)
[英]How to fix org.hibernate.MappingException unmapping class
[英]How to fix org.hibernate.MappingException?
我是 JPA 的新手,在嘗試設置 UserContact 實體時出現此錯誤。
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: USER_ID, for columns: [org.hibernate.mapping.Column(userContact)]
我有 2 個實體類和一個用於復合鍵的 @Embeddable 類。 這個問題似乎有很多解決方案,所以我在 getter/setter 和字段中混合和匹配了屬性。 我試過@JsonBackReference 和@JsonManagedReference、@ElementCollection 和其他注釋。 使用@Access(AccessType.PROPERTY) 確實啟動了服務器,但是在嘗試執行數據庫操作時給了我這個錯誤。
org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role:
任何幫助,將不勝感激。 這是我的實體。
用戶
@Entity
@Table(name = "USER_RECORD")
public class User {
private UserRecordId id;
private String name;
private String address;
@Column
@ElementCollection(targetClass=UserContact.class)
private Set<UserContact> userContact = new HashSet<UserContact>(0);
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "userId", column = @Column(name = "USER_ID", nullable = false)),
@AttributeOverride(name = "userId2", column = @Column(name = "USER_ID2", nullable = false)) })
public UserRecordId getId() {
return this.id;
}
public void setId(UserRecordId id) {
this.id = id;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
public Set<UserContact> getUserContact() {
return this.userContact;
}
public void setUserContact(Set<UserContact> userContact) {
this.userContact = userContact;
}
@Column(name = "USER_NAME", nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "USER_ADDRESS", nullable = false)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
用戶聯系方式
@Entity
@Table(name = "USER_CONTACT")
public class UserContact {
private String userContactId;
private String name;
private String country;
private User user;
@Id
@Column(name = "USER_CONTACT_ID", unique = true, nullable = false)
public String getUserContactId() {
return this.userContactId;
}
public void setUserContactId(String userContactId) {
this.userContactId = userContactId;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID"),
@JoinColumn(name = "USER_ID2", referencedColumnName = "USER_ID2") })
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
@Column(name = "CONTACT_NAME", nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "CONTACT_COUNTRY", nullable = false)
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
用戶記錄 ID/可嵌入
@Embeddable
public class UserRecordId
private String userId;
private String userId2;
@Column(name = "USER_ID", nullable = false)
public String getUserId() {
return this.userId;
}
.../////getUserid2
......
.....
override equals & hash code
您似乎已經注釋了一個字段和一個 getter (userContact)。 您應該使用 FIELD 或 PROPERTY 訪問,但不能同時使用(尤其是對於同一個字段!)。
此外,您還一次將其注釋為ElementCollection
,一次將其注釋為OneToMany
。 當元素是實體時,不能兩者兼而有之,當然也不能是ElementCollection
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.