java - org.hibernate.MappingException : Could not determine type for:

标签 java hibernate

我使用额外字段为多对多实现了此解决方案: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/

相关文章:

java - 无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery

Java:更好地理解枚举

sql - Hibernate等于 "select 1 from DUAL"是什么?

mysql - 为什么我在本地 TomEE 1.6 中收到 HsqlException?

Java Hibernate 级联问题

java - 无法在 Android 中显示谷歌地图

java - 是否有一些更好的方法来使用 PdfStripper 转换 pdf 的字节数组?

java - 如何在编辑时更改 JTable 单元格文本?

Java 调试 : Source not found

c++ - 类似于 C++ 的 Hibernate 层