我使用额外字段为多对多实现了此解决方案: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 函数有关。
最佳答案
我也遇到了和你一样的问题,通过将注解移到对应的get方法前面解决了。
关于java - org.hibernate.MappingException : Could not determine type for:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202566/