如何用非主键在Hibernate中处理多对多关系?

时间:2019-01-25 18:48:36

标签: java spring hibernate spring-boot many-to-many

我有两个对象-角色和权限。他们有多对多的关系。每个对象的代码如下所示。

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.NaturalId;

@Entity
@Table(name = "rml_privilege")
public class RmlPrivilege extends RmlBase implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -6810028565979325754L;

    public static final String ATTR_NAME = "name";


    @Column(name="name",nullable=false,unique=true)
    private String name;

    @Column(name="description")
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String privilege) {
        this.name = privilege;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

这是RmlRole文件。

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "rml_role")
public class RmlRole extends RmlBase {

    public final static String ATTR_ORG = "org.id";

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "organization")
    private RmlOrganization organization;

    //TODO: In case of LAZY loading, recieve an error
    @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinTable(name = "rml_role_privilege", joinColumns = {
            @JoinColumn(name = "role", referencedColumnName = "id") }, inverseJoinColumns = {
                    @JoinColumn(name = "privilege", referencedColumnName = "name") })
    Set<RmlPrivilege> privileges;

    public RmlOrganization getOrganization() {
        return organization;
    }

    public void setOrganization(RmlOrganization organization) {
        this.organization = organization;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<RmlPrivilege> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(Set<RmlPrivilege> privileges) {
        this.privileges = privileges;
    }

}

当我尝试获取RmlRole对象时,最终得到的“ privileges”变量为空。我希望可以准备好特权对象集。我在某个地方做错了,但无法识别。

我的Hibernate版本:5 Java 1.8 Spring Boot应用程序

任何帮助将不胜感激。

0 个答案:

没有答案