使用where子句将Spring Data JPA中的两个表实体连接起来

时间:2017-12-19 08:59:16

标签: java spring hibernate jpa

我一直在寻找下面我的问题的答案,到目前为止没有任何结果。

快速解释:

一个人可以拥有多个角色,角色与特权有多对多的关系。

我有实体,角色

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "ROLE")
@Where(clause = "RECORD_STATUS = 'A'")
@SQLDelete(sql = "UPDATE ROLE SET RECORD_STATUS = 'D' where OBJID = ?")
public class Role extends BaseEntity implements Serializable {

   private String roleDescription;
   private String roleName;



   @JoinTable(
        name = "ROLE_PRIVILEGE",
        joinColumns = @JoinColumn(
                name = "roleOid"
        ),
        inverseJoinColumns = @JoinColumn(
                name = "privilegeOid"
        )
    )
    @Where(clause = "RECORD_STATUS = 'A'")
    @ManyToMany(fetch = FetchType.EAGER)
    List<Privilege> privileges;
}

权限实体:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "PRIVILEGE")
@Where(clause = "RECORD_STATUS = 'A'")
@SQLDelete(sql = "UPDATE PRIVILEGE SET RECORD_STATUS = 'D' where OBJID = ?")
public class Privilege extends BaseEntity implements Serializable {

   private String description;
   private String privilegeName;



}

我想加入这两个表:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "ROLE_PRIVILEGE")
@Where(clause = "RECORD_STATUS = 'A'")
@SQLDelete(sql = "UPDATE ROLE_PRIVILEGE SET RECORD_STATUS = 'D' where OBJID 
= ?")
public class RolePrivilege extends BaseEntity implements Serializable {

   private String roleOid;
   private String privilegeOid;

}

问题是,当我试图获得一个人的角色时,他们都拥有特权。但是,hibernate并不关心ROLE_PRIVILEGE权利中的where子句:

select
 privileges0_.role_oid as role_oid8_23_0_,
 privileges0_.privilege_oid as privileg7_23_0_,
 privilege1_.objid as objid1_21_1_,
 privilege1_.creation_date as creation2_21_1_,
 privilege1_.last_change_date as last_cha3_21_1_,
 privilege1_.luc as luc4_21_1_,
 privilege1_.record_status as record_s5_21_1_,
 privilege1_.user_created as user_cre6_21_1_,
 privilege1_.description as descript7_21_1_,
 privilege1_.privilege_name as privileg8_21_1_
from
 role_privilege privileges0_
 inner join
  privilege privilege1_
   on privileges0_.privilege_oid=privilege1_.objid
where
(
  privilege1_.RECORD_STATUS = 'A'
)
and privileges0_.role_oid=?

我的意思是,当我停用角色权限元组(角色不再具有该权限)时,仍然会找到我。在哪里可以将@where子句添加到ROLE_PRIVILEGE表中?

所有建议都很受欢迎!

1 个答案:

答案 0 :(得分:0)

我真的不知道我是如何错过的,但找到了解决方案。

@WhereJoinTable(clause = "RECORD_STATUS = 'A' ")

JPA @JoinTable with extra join conditions