我使用exstra字段为多对多人实施了此解决方案:Many to Many Hibernate Mapping for additional property in the join table
我的代码:
@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函数有关。
答案 0 :(得分:0)
我也遇到了和你一样的问题,我通过将注释移到相应的get方法的前面来解决它。