Java 联接表上的Hibernate条件显示意外结果

Java 联接表上的Hibernate条件显示意外结果,java,hibernate,join,criteria,Java,Hibernate,Join,Criteria,我有这样一个标准: public List<role> searchByFormStatus(boolean status) { Criteria criteria = this.getSession().createCriteria(this.getPersistentClass()); List<role> result = (List<role>) criteria

我有这样一个标准:

public List<role> searchByFormStatus(boolean status) {
    Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());
    List<role> result = (List<role>) criteria                               
                        .setFetchMode("role", FetchMode.JOIN)
                        .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
                        .createAlias("formApprovals", "f")
                        .add(Restrictions. eq("f.latestApproval", true))
                        .list();
    return result;                          
}
正如您所看到的,“FormApproval”会带来数据库中的所有寄存器,即使我为latestApproval属性创建了限制

父对象(角色)中的属性声明为:

角色类

@Entity
@Table(name="Role"
    ,catalog="catalog"
)    
public class Role  implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private int idRole;
     private WorkType workType;
     private String roleName;
     private String roleNotes;
     private boolean roleIsActive;
     private Set<FormApproval> formApprovals = new HashSet<FormApproval>(0);
     private Set<Topicrole> topicRoles = new HashSet<TopicRole>(0);
     private Set<FormFeedBack> formFeedBacks = new HashSet<FormFeedBack>(0);
     private Set<UserRole> userRoles = new HashSet<UserRrole>(0);
     private Set<Interview> interviews = new HashSet<Interview>(0);

    public Role() {
    }


    public Role(int idRole, WorkType workType, String roleName, boolean roleIsActive) {
        this.idRole = idRole;
        this.workType = workType;
        this.RoleName = RoleName;
        this.roleIsActive = roleIsActive;
    }
    public Role(int idRole, WorkType workType, String roleName, String roleNotes, boolean roleIsActive, Set<FormApproval> formApprovals, Set<TopicRole> topicRoles, Set<FormFeedBack> formFeedBacks, Set<UserRole> userRoles, Set<Interview> interviews) {
       this.idRole = idRole;
       this.workType = workType;
       this.RoleName = RoleName;
       this.roleNotes = roleNotes;
       this.roleIsActive = roleIsActive;
       this.formApprovals = formApprovals;
       this.topicRoles = topicRoles;
       this.formFeedBacks = formFeedBacks;
       this.userRoles = userRoles;
       this.interviews = interviews;
    }

     @Id @GeneratedValue(strategy = IDENTITY) 

    @Column(name="IDRole", unique=true, nullable=false)
    public int getIdrole() {
        return this.idRole;
    }

    public void setIdrole(int idRole) {
        this.idRole = idRole;
    }
@ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="FKWorkType", nullable=false)
    public WorkType getWorkType() {
        return this.workType;
    }

    public void setWorkType(WorkType workType) {
        this.workType = workType;
    }

    @Column(name="RoleName", nullable=false)
    public String getRoleName() {
        return this.roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @Column(name="RoleNotes")
    public String getRoleNotes() {
        return this.roleNotes;
    }

    public void setRoleNotes(String roleNotes) {
        this.roleNotes = roleNotes;
    }

    @Column(name="RoleIsActive", nullable=false)
    public boolean isRoleIsActive() {
        return this.roleIsActive;
    }

    public void setRoleIsActive(boolean roleIsActive) {
        this.roleIsActive = roleIsActive;
    }

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="role")
    public Set<FormApproval> getFormApprovals() {
        return this.formApprovals;
    }

    public void setFormApprovals(Set<FormApproval> formApprovals) {
        this.formApprovals = formApprovals;
    }

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="role")
    public Set<TopicRole> getTopicRoles() {
        return this.topicRoles;
    }

    public void setTopicRoles(Set<TopicRole> topicRoles) {
        this.topicRoles = topicRoles;
    }

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="role")
@JsonManagedReference
    public Set<FormFeedBack> getFormFeedBacks() {
        return this.formFeedBacks;
    }

    public void setFormFeedBacks(Set<FormFeedBack> formFeedBacks) {
        this.formFeedBacks = formFeedBacks;
    }

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="role")
    public Set<UserRole> getUserRoles() {
        return this.userRoles;
    }

    public void setUserRoles(Set<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="acnrole")
    public Set<Interview> getInterviews() {
        return this.interviews;
    }

    public void setInterviews(Set<Interview> interviews) {
        this.interviews = interviews;
    }
@实体
@表(name=“Role”
,catalog=“catalog”
)    
公共类角色实现java.io.Serializable{
私有静态最终长serialVersionUID=1L;
私人互联网;
私人工作类型工作类型;
私有字符串roleName;
私有字符串roleNotes;
私有布尔角色激活;
private Set formApprovals=新哈希集(0);
私有集topicRoles=新哈希集(0);
私有集formFeedBacks=新哈希集(0);
私有集userRoles=newhashset(0);
私有集访问=新哈希集(0);
公共角色(){
}
公共角色(int-idRole、WorkType-WorkType、String-roleName、boolean-roleIsActive){
this.idRole=idRole;
this.workType=工作类型;
this.RoleName=RoleName;
this.roleIsActive=roleIsActive;
}
公共角色(int-idRole、WorkType-WorkType、String-roleName、String-rolenates、boolean-roleIsActive、Set-formApprovals、Set-topicRoles、Set-formFeedBacks、Set-userRoles、Set-interview){
this.idRole=idRole;
this.workType=工作类型;
this.RoleName=RoleName;
this.roleNotes=roleNotes;
this.roleIsActive=roleIsActive;
this.formApprovals=formApprovals;
this.topicRoles=topicRoles;
this.formFeedBacks=formFeedBacks;
this.userRoles=userRoles;
这个。面试=面试;
}
@Id@GeneratedValue(策略=标识)
@列(name=“IDRole”,unique=true,nullable=false)
public int getIdrole(){
返回此.idRole;
}
公共void setIdrole(int idRole){
this.idRole=idRole;
}
@manytone(fetch=FetchType.EAGER)
@JoinColumn(name=“FKWorkType”,nullable=false)
公共工作类型getWorkType(){
返回此.workType;
}
公共void集合工作类型(工作类型工作类型){
this.workType=工作类型;
}
@列(name=“RoleName”,null=false)
公共字符串getRoleName(){
返回此.roleName;
}
public void setRoleName(字符串roleName){
this.roleName=roleName;
}
@列(name=“RoleNotes”)
公共字符串getRoleNotes(){
返回此.roleNotes;
}
公共无效集合节点(字符串集合节点){
this.roleNotes=roleNotes;
}
@列(name=“RoleIsActive”,null=false)
公共布尔值isRoleIsActive(){
返回此.roleIsActive;
}
public void setRoleIsActive(布尔值roleIsActive){
this.roleIsActive=roleIsActive;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=“role”)
公共集getFormApprovals(){
将此文件返回。formApprovals;
}
公共作废设置格式批准(设置格式批准){
this.formApprovals=formApprovals;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=“role”)
公共集getTopicRoles(){
返回此.topicRoles;
}
公共无效settopicoles(settopicoles){
this.topicRoles=topicRoles;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=“role”)
@JsonManagedReference
公共集getFormFeedBacks(){
返回此文件,形成反馈;
}
公共无效setFormFeedBacks(setFormFeedBacks){
this.formFeedBacks=formFeedBacks;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=“role”)
公共集合getUserRoles(){
返回this.userRoles;
}
public void setUserRoles(Set userRoles){
this.userRoles=userRoles;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=“acnrole”)
公共集getjournals(){
把这封信还给我;
}
公开采访(现场采访){
这个。面试=面试;
}

}

对于急取类型,嵌套条件应适用于这种情况:

public List<role> searchByFormStatus(boolean status) {
    Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());
    List<role> result = (List<role>) criteria                               
                        .createCriteria("formApprovals")
                             .add(Restrictions.eq("latestApproval", true))
                        .list();
    return result;                          
}
公共列表searchByFormStatus(布尔状态){
Criteria=this.getSession().createCriteria(this.getPersistentClass());
列表结果=(列表)标准
.createCriteria(“正式批准”)
.add(Restrictions.eq(“latestApproval”,true))
.list();
返回结果;
}

您可以尝试将限制用作关联顶部的
ON
子句的一部分

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="role")
@Where(clause=" latestApproval='true' ")
public Set<FormApproval> getFormApprovals() {
    return this.formApprovals;
}
更新:

对于hibernate的旧版本,请使用下面的其他方法。其中,
joinType
对于
LEFT\u OUTER\u JOIN
来说,值应该是1,并且JOIN子句可以作为第四个参数传递

public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
固定使用

.createCriteria("formApprovals","f",1,Restrictions.eq("f.latestApproval", status))

1强制在查询中使用左外联接

是否可以在问题中添加带批注的FormApproval字段?为什么需要设置获取模式和结果转换器?@johncastergood done!fetchmode和resulttransformer存在,因为我在使用内部联接生成的查询中遇到问题,但我需要左外部联接,这解决了您需要左外部联接的问题,因为role.formApprovals可能为null?您是否尝试将获取模式添加为createAlias()的一部分?尝试在FormApproval实体中使用Boolean而不是Boolean以及相应的setter和getter,我只是猜测这可能会有帮助,也可能没有帮助。是的,尝试了FetchMode.EAGER,但表示它已被弃用,因此我使用了JOIN,看起来它与参数不匹配(String,String,FetchMode)。它会不断检索所有FormApproval数据,即使是那些具有latestApproval!=trueyes的数据,就像在某些情况下被覆盖一样,当我将JoinType.LEFT\u OUTER\u JOIN添加到createAlias时,我得到一个错误,argu
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="role")
@Where(clause=" latestApproval='true' ")
public Set<FormApproval> getFormApprovals() {
    return this.formApprovals;
}
public List<role> searchByFormStatus(boolean status) {
    Criteria criteria = this.getSession().createCriteria(Role.class, "role")
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
            .createAlias("role.formApprovals", "formApprovals", JoinType.LEFT_OUTER_JOIN);
    return criteria.list();
}
public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
.createCriteria("formApprovals","f",1,Restrictions.eq("f.latestApproval", status))