JPA2中的多对多映射用于具有复合键的实体

时间:2016-04-19 13:22:07

标签: java spring hibernate jpa

我正在为具有复合键的实体执行多对多映射,但我在“字段列表”中收到异常未知列'rightlist0_.right_list_Company_Id'。请帮忙。下面是我的实体类和映射。提前谢谢。

 @Entity
 @Table(name = "tblrole")
 public class Role {
 @Column(name = "Role_Name")
 String roleName;

 @Column(name = "Role_Id", insertable = false, updatable = false)
 Integer roleId;

 @Column(name = "Company_Id", insertable = false, updatable = false)
 Integer companyId;

 @EmbeddedId
 RolePk rolePk;


 @ManyToMany(fetch = FetchType.LAZY)
 @JoinTable(name = "tblroleright", 
 joinColumns = {
 @JoinColumn(name = "Role_Id", insertable = false, updatable = false),
 @JoinColumn(name = "Company_Id", insertable = false, 
 updatable = false) })
 List<Right> rightList;
}



 @Embeddable
 public class RolePk implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Column(name = "Role_Id")
Integer roleId;

@Column(name = "Company_Id")
Integer companyId;

public Integer getRoleId() {
    return roleId;
}

public void setRoleId(Integer roleId) {
    this.roleId = roleId;
}

public Integer getCompanyId() {
    return companyId;
}

public void setCompanyId(Integer companyId) {
    this.companyId = companyId;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result +
    ((companyId == null) ? 0 :  companyId.hashCode());
    result = prime * result +  
    ((roleId == null) ? 0 :  roleId.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    RolePk other = (RolePk) obj;
    if (companyId == null) {
        if (other.companyId != null)
            return false;
    } else if (!companyId.equals(other.companyId))
        return false;
    if (roleId == null) {
        if (other.roleId != null)
            return false;
    } else if (!roleId.equals(other.roleId))
        return false;
    return true;
}

public RolePk(Integer roleId, Integer companyId) {
    super();
    this.roleId = roleId;
    this.companyId = companyId;
}

public RolePk() {
    super();
    // TODO Auto-generated constructor stub
}

}


@Entity
@Table(name = "tblright")
public class Right {

@Column(name = "Right_Name")
String rightName;

@Column(name = "Right_Id", insertable = false, updatable = false)
Integer rightId;

@Column(name = "Company_Id", insertable = false, updatable = false)
Integer companyId;

@EmbeddedId
RightPk rightPk;

public String getRightName() {
    return rightName;
}

/*@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tblroleright",  
joinColumns = {
@JoinColumn(name = "Right_Id", insertable = false, updatable = false),
@JoinColumn(name = "Company_Id", insertable = false, 
updatable =  false) })
List<Role> roleList;*/

public void setRightName(String rightName) {
    this.rightName = rightName;
}

public Integer getRightId() {
    return rightId;
}

public void setRightId(Integer rightId) {
    this.rightId = rightId;
}

/*public List<Role> getRoleList() {
    return roleList;
}

public void setRoleList(List<Role> roleList) {
    this.roleList = roleList;
}*/

public Integer getCompanyId() {
    return companyId;
}

public void setCompanyId(Integer companyId) {
    this.companyId = companyId;
}

public RightPk getRightPk() {
    return rightPk;
}

public void setRightPk(RightPk rightPk) {
    this.rightPk = rightPk;
}
}

@Embeddable
public class RightPk implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Column(name = "Right_Id")
Integer rightId;

@Column(name = "Company_Id")
Integer companyId;



public Integer getRightId() {
    return rightId;
}

public void setRightId(Integer rightId) {
    this.rightId = rightId;
}

public Integer getCompanyId() {
    return companyId;
}

public void setCompanyId(Integer companyId) {
    this.companyId = companyId;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + 
    ((companyId == null) ? 0 :  companyId.hashCode());
    result = prime * result +
    ((rightId == null) ? 0 : rightId.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    RightPk other = (RightPk) obj;
    if (companyId == null) {
        if (other.companyId != null)
            return false;
    } else if (!companyId.equals(other.companyId))
        return false;
    if (rightId == null) {
        if (other.rightId != null)
            return false;
    } else if (!rightId.equals(other.rightId))
        return false;
    return true;
}



public RightPk(Integer rightId, Integer companyId) {
    super();
    this.rightId = rightId;
    this.companyId = companyId;
}

public RightPk() {
    super();
    // TODO Auto-generated constructor stub
}

}

1 个答案:

答案 0 :(得分:0)

首先,您不需要在实体中复制PK的内容。 其次,您不能使用复合ID创建多对多关联,您必须创建一个与您的两个类相关的新实体RoleRight,其中包含一对多关联,包含Role和Right Pks。