【问题标题】:org.hibernate.MappingException: Could not determine type for:org.hibernate.MappingException:无法确定类型:
【发布时间】:2014-11-29 21:44:54
【问题描述】:

我已经为多对多的额外字段实现了这个解决方案: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 函数有关。

【问题讨论】:

  • 你猜对了。使用@Transient 对其进行注释。为什么使用复合主键让自己的生活变得复杂?在 UserRole 实体中拥有一个纯技术的、自动生成的单列 ID 会简单得多。

标签: java hibernate


【解决方案1】:

我也遇到了和你一样的问题,我把注解移到对应get方法的前面解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2013-01-25
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多