如何修复org.hibernate.MappingException?

时间:2015-08-28 09:19:13

标签: java hibernate jpa jackson

我是JPA新手并在尝试设置UserContact Entity时遇到此错误。

 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;
}

UserContact

@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;
}

UserRecordId /可嵌入

@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

1 个答案:

答案 0 :(得分:1)

您好像已经注释了一个字段和一个getter(userContact)。您应该使用FIELD或PROPERTY访问,但不能同时使用两者(特别是对于同一个字段!)。

此外,您已将其标注为ElementCollection一次,并将其标注为OneToMany。不能同时存在,当元素是实体时,当然不能是ElementCollection