休眠映射复合键 [英] Hibernate mappedby composite key

查看:106
本文介绍了休眠映射复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用@ OneToMany中的映射,但它引发异常

  org.hibernate.MappingException:无法读取org.caau.entity.UserModuleRole中的moduleRoles的映射属性! 

这里是我的实体映射.UserModuleRole是复合类。如果使用注释代码,程序是正确的。有人帮我解决了吗?

  @Entity 
@Table(name =user)
公共类用户{
私人长ID;
private Set< UserModuleRole> moduleRoles;

@Id
@Column(name =id,nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId(){
return id;
}

@OneToMany(fetch = FetchType.EAGER,mappedBy =user)
//如果在代码中使用,程序是正确的,但是我想使用mappedBy!
// @ OneToMany(fetch = FetchType.EAGER)
// @ JoinColumn(name =user_id)
public Set< UserModuleRole> getModuleRoles(){
return moduleRoles;
}

}

这里是类UserModuleRole

  @Entity 
@Table(name =user_module_role)
@IdClass(UserModuleRolePK.class)
公共类UserModuleRole {
私人用户用户;
private ModuleRole moduleRole;
private long addUserId;
私人日期addDate;
private long updateUserId;
私人日期updateDate;

@Id
public User getUser(){
return user;
}

@Id
public ModuleRole getModuleRole(){
return moduleRole;
}
}
class UserModuleRolePK实现可序列化{

private static final long serialVersionUID = -9132981262254922539L;

私人用户用户;
private ModuleRole moduleRole;

@ManyToOne
@JoinColumn(name =user_id,nullable = false)
public User getUser(){
return user; $ {
}

@ManyToOne
@JoinColumns({@JoinColumn(name =module_id,nullable = false),@JoinColumn(name =role_id,nullable = false) })
public ModuleRole getModuleRole(){
return moduleRole;




解决方案 UserModuleRole user c $ c> class。



您试图实现的是您的实体多对多关系> User 和 ModuleRole ,您的示例中不需要中介类 UserModuleRole 只有字段/属性是其他两个实体的外键引用。



除非您忘记在问题中添加更多信息,否则您应该使用的映射太多更简单,如下所示:

  @Entity 
公共类ModuleRole {

私有长ID;

私人设定<使用者>用户;

@Id
@Column(name =id)
public Long getId(){
return id;
}

@ManyToMany
@JoinTable(name =user_module_role,
joinColumns = @JoinColumn(name =module_id,referencedColumnName =id),
inverseJoinColumns = @JoinColumn(name =user_id),referencedColumnName =id)
public Set< Users> getUsers(){
返回用户;



$ b实际
@Table(name =user)
public class User {
private长ID;
private Set< ModuleRole> moduleRoles;

@Id
@Column(name =id,nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId(){
return id;
}

@ManyToMany(mappedBy =users)
public Set< ModuleRole> getModuleRoles(){
return moduleRoles;
}

}

Hibernate将处理中间表除非你想为关系添加额外的字段,否则你不必担心它,在这种情况下,本教程将有所帮助。


I want to use mapped in @OneToMany,but it throws an exception

org.hibernate.MappingException: Unable to read the mapped by attribute for moduleRoles in org.caau.entity.UserModuleRole!

And here is my entity mapping.UserModuleRole is composite class.if use comment code ,program is correct.Does anyone help me to solve?

@Entity
@Table(name = "user")
public class User{
    private long id;
    private Set<UserModuleRole> moduleRoles;

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    @OneToMany(fetch = FetchType.EAGER,mappedBy="user")
    //if use under code,program is correct,but i want to use mappedBy!
    //@OneToMany(fetch = FetchType.EAGER)
    //@JoinColumn(name = "user_id")
    public Set<UserModuleRole> getModuleRoles() {
        return moduleRoles;
    }

}

here is class UserModuleRole

 @Entity
    @Table(name = "user_module_role")
    @IdClass(UserModuleRolePK.class)
        public class UserModuleRole{
        private User user;
        private ModuleRole moduleRole;
        private long addUserId;
        private Date addDate;
        private long updateUserId;
        private Date updateDate;

        @Id
        public User getUser() {
            return user;
        }

        @Id
        public ModuleRole getModuleRole() {
            return moduleRole;
        }
    }
    class UserModuleRolePK implements Serializable {

        private static final long serialVersionUID = -9132981262254922539L;

        private User user;
        private ModuleRole moduleRole;

        @ManyToOne
        @JoinColumn(name = "user_id", nullable = false)
        public User getUser() {
            return user;
        }

        @ManyToOne
        @JoinColumns({ @JoinColumn(name = "module_id", nullable = false), @JoinColumn(name = "role_id", nullable = false) })
        public ModuleRole getModuleRole() {
            return moduleRole;
        }
    }
    }

解决方案

First, the exception you are getting is due to the fact that there is not any relational attribute named user in your UserModuleRole class.

What you are trying to achieve is a Many-To-Many relationship between your entities User and ModuleRole, the intermediary class UserModuleRole is not needed in your example as the only fields/properties it holds are the foreign key references to the other two entities.

Unless you forgot to add more information to your question, the mapping you should use is much simpler, something like the following:

@Entity
public class ModuleRole {

    private Long id;

    private Set<User> users;

    @Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    @ManyToMany
    @JoinTable(name = "user_module_role", 
        joinColumns = @JoinColumn(name = "module_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "user_id"), referencedColumnName = "id")
    public Set<Users> getUsers() {
        return users;
    }

}

@Entity
@Table(name = "user")
public class User{
    private Long id;
    private Set<ModuleRole> moduleRoles;

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    @ManyToMany(mappedBy = "users")
    public Set<ModuleRole> getModuleRoles() {
        return moduleRoles;
    }

}

Hibernate will deal with the intermediary table on its own, you don't have to worry about it unless you want to add additional fields to the relationship, in that case this tutorial would help.

这篇关于休眠映射复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆