简体   繁体   中英

How to fix org.hibernate.MappingException?

I'm new to JPA and getting this error when trying to set 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)]

I have 2 Entity Classes and one @Embeddable class for composite key. There seems to be many solutions to this problem so I've mixed and matched attributes along getters/setters and fields. I've tried @JsonBackReference and @JsonManagedReference, @ElementCollection and other annotations. Using @Access(AccessType.PROPERTY) did start the server correctly but gave me this error when trying to perform db operation.

 org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role:

Any help would be appreciated. Here are my Entities.

User

@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

@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

You seem to have annotated a field AND a getter (userContact). You should use either FIELD or PROPERTY access but not both (particularly for the same field!).

Also you have annotated it once as ElementCollection and once as OneToMany . Can't be both, and certainly can't be ElementCollection when the element is an Entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM