1
我實現了這個解決方案爲多對多與exstra領域:Many to Many Hibernate Mapping for additional property in the join tableorg.hibernate.MappingException:無法確定類型:
我的代碼:
@Entity
public class User {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user", cascade = CascadeType.ALL)
private Set<UserRole> userRoles;
}
@Entity
public class Role {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.role", cascade = CascadeType.ALL)
private Set<UserRole> userRoles;
}
@Entity
@AssociationOverrides({ @AssociationOverride(name = "pk.user", joinColumns = @JoinColumn(name = "user_id")),
@AssociationOverride(name = "pk.role", joinColumns = @JoinColumn(name = "role_id")) })
public class UserRole{
private UserRoleId pk;
public UserRole(User user, Role role) {
super();
this.pk = new UserRoleId(
user, role);
}
public UserRole() {
super();
}
public Long getUserId() {
return this.pk.getUser().getId();
}
@EmbeddedId
public UserRoleId getPk() {
return pk;
}
public void setPk(UserRoleId pk) {
this.pk = pk;
}
public User getUser() {
return this.pk.getUser();
}
public Role getRole() {
return this.pk.getRole();
}
}
@SuppressWarnings("serial")
@Embeddable
public class UserRoleId implements Serializable {
private User user;
private Role role;
public UserRoleId() {
super();
}
public UserRoleId(User user, Role role) {
super();
this.user = user;
this.role = role;
}
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
我得到這個錯誤:
Caused by: org.hibernate.MappingException: Could not determine type for: com.xxx.model.entities.User, at table: UserRole, for columns: [org.hibernate.mapping.Column(user)]
我猜它與UserRole實體中的getUser函數有關。
你的猜測是正確的。用@Transient對它進行註釋。爲什麼使用複合主鍵讓自己的生活變得複雜?在UserRole實體中擁有一個純技術的自動生成的單列ID會簡單得多。 – 2014-10-05 12:35:57