简体   繁体   English

通过复合键映射的Hibernate

[英]Hibernate mappedby composite key

I want to use mapped in @OneToMany,but it throws an exception 我想在@OneToMany中使用映射,但是会引发异常

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? 这是我的实体映射。UserModuleRole是复合类。如果使用注释代码,程序是正确的。有人帮我解决吗?

@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 这是类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. 首先,您得到的异常是由于在UserModuleRole类中没有任何名为user关系属性。

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. 您试图实现的是实体UserModuleRole之间的多对多关系,在您的示例中不需要中间类UserModuleRole ,因为它拥有的唯一字段/属性是对其他两个实体的外键引用。

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. Hibernate将自己处理中介表,除非您想向关系中添加其他字段,否则您不必担心它,在这种情况下, 本教程会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM