虽然我们的条件没有得到满足,但Hibernate标准会加载所有子数据

时间:2017-05-29 11:48:11

标签: oracle hibernate hibernate-criteria

我有三个实体:

 Organization
     - Id
     - visibilityFlag
     - List<BusinessGroup> tblmorgbusinessgroups

   BusinessGroup
     - Id
     - visibilityFlag
     - orgId
     - List<BusinessHierarchy> tblmorgbusinessgrouphies

   BusinessHierarchy
     - Id
     - visibilityFlag
     - bgId

现在我想找出

的记录
org.id = "1"
org.visibilityFlag = "0"
bg.visibilityFlag = "0"
bh.visibilityFlag = "0"

我正在组织上创建crieteria,因此它正在加载BusinessGroup和BusinessHierarchy的所有记录,如果它们满足上述条件的话。

Criteria organizationDOCriteria = sessionFactory.getCurrentSession().createCriteria(OrganizationDO.class);
organizationDOCriteria.createAlias("tblmorgbusinessgroups", "businessGroup");
organizationDOCriteria.createAlias("businessGroup.tblmorgbusinessgrouphies", "businessGroupHierarchy");
organizationDOCriteria.add(Restrictions.eq("organizationid", organizationId));
organizationDOCriteria.add(Restrictions.eq("businessGroup.visibilityflag", visibilityFlag));
organizationDOCriteria.add(Restrictions.eq("businessGroupHierarchy.visibilityflag", visibilityFlag));
organizationDOCriteria.add(Restrictions.eq("visibilityflag", visibilityFlag));
Object organizationDOObj = organizationDOCriteria.uniqueResult();

我想加载具有visibilityFlag =&#34; 0&#34 ;;

的BusinessGroup /业务层次结构
-------------------------------------------------
Current Data
-------------------------------------------------
Orgid   visibilityFlag  Name
1       0               Org1

BusinessGroup
id  visibilityFlag  Name    orgId
1   0               Bg1     1
2   3               Bg2     1
3   0               Bg3     1

BusinessHierarchy
id  visibilityFlag  Name    bgId
1   0               BH1     1
2   0               BH2     2
3   0               BH3     1
4   3               BH4     1
5   0               BH5     3
6   0               BH6     3
7   0               BH7     2

-------------------------------------------------
Expected Output
-------------------------------------------------
Orgid   visibilityFlag  Name
1       0               Org1

BusinessGroup
id  visibilityFlag  Name    orgId
1   0               Bg1     1
3   0               Bg3     1

BusinessHierarchy
id  visibilityFlag  Name    bgId
1   0               BH1     1
3   0               BH3     1
5   0               BH5     3
6   0               BH6     3

-------------------------------------------------
Actual Output Data
-------------------------------------------------
Orgid   visibilityFlag  Name
1       0               Org1

BusinessGroup
id  visibilityFlag  Name    orgId
1   0               Bg1     1
2   3               Bg2     1
3   0               Bg3     1

BusinessHierarchy
id  visibilityFlag  Name    bgId
1   0               BH1     1
2   0               BH2     2
3   0               BH3     1
4   3               BH4     1
5   0               BH5     3
6   0               BH6     3
7   0               BH7     2

数据对象详情

OrganizationDO

package com.xxx.xxx.xxx.dataobject;

//Import statements

/**
 * The persistent class for the XXXTABLE database table.
 * 
 */
@Entity
@Table(name="XXXTABLE")
public class OrganizationDO implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_XXXTABLE")
    @SequenceGenerator(name="SEQ_XXXTABLE",sequenceName="SEQ_XXXTABLE",allocationSize = 1, initialValue= 1 )
    private Long organizationid;

    private String name;

    private String visibilityflag;

    //bi-directional many-to-one association to OrgBusinessGroupDO
    @OneToMany(mappedBy="tblmorganization", cascade=CascadeType.ALL)
    private List<OrgBusinessGroupDO> tblmorgbusinessgroups;

//Getter Setter methods
}

BusinessGroupDO

package com.xxx.xxx.xxx.dataobject;

//Import statements

/**
 * The persistent class for the XXXBUSGRPTABLE database table.
 * 
 */
@Entity
@Table(name="XXXBUSGRPTABLE")
public class OrgBusinessGroupDO implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_XXXBUSGRPTABLE")
    @SequenceGenerator(name="SEQ_XXXBUSGRPTABLE",sequenceName="SEQ_XXXBUSGRPTABLE",allocationSize = 1, initialValue= 1 )
    private Long businessgroupid;

    private String name;

    private String visibilityflag;

    @ManyToOne
    @JoinColumn(name="ORGANIZATIONID")
    private OrganizationDO tblmorganization;

    //bi-directional many-to-one association to OrgBusinessGroupHierarchyDO
    @OneToMany(mappedBy="tblmorgbusinessgroup")
    private List<OrgBusinessGroupHierarchyDO> tblmorgbusinessgrouphies;

    //Getters/setters and toString/hashcode
}

BusinessGroupHierarchyDO

package xxx.xxx.xxx.xxx.dataobject;

//Import statements

/**
 * The persistent class for the XXXBUSINESSGRPHIETABLE database table.
 * 
 */
@Entity
@Table(name="XXXBUSINESSGRPHIETABLE")
public class OrgBusinessGroupHierarchyDO implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_XXXBUSINESSGRPHIETABLE")
    @SequenceGenerator(name="SEQ_XXXBUSINESSGRPHIETABLE",sequenceName="SEQ_XXXBUSINESSGRPHIETABLE",allocationSize = 1, initialValue= 1 )
    private Long hierarchyid;

    private String name;

    private String visibilityflag;

    //bi-directional many-to-one association to OrgBusinessGroupDO
    @ManyToOne
    @JoinColumn(name="BUSINESSGROUPID")
    private OrgBusinessGroupDO tblmorgbusinessgroup;

    //bi-directional many-to-one association to OrgBusinessGroupHierarchyDO
    @OneToMany(mappedBy="tblmorgbusinessgrouphie")
    private List<OrgBusinessGroupHierarchyDO> tblmorgbusinessgrouphies;
}

0 个答案:

没有答案