hibernate:如何在父级时获取子集合

时间:2013-02-21 09:22:04

标签: hibernate

我有casemaster(DLawCaseMaster - parent)类和casemaster历史记录(DLawCaseMasterH - child)类。我有需要父母和孩子收集的情况。

DLawCaseMaster.java

public class DLawCaseMaster implements java.io.Serializable {
    //.....
    private Set<DLawCaseMasterH> DLawCaseMasterHs = new HashSet<DLawCaseMasterH>(0);
    //.....

    public Set<DLawCaseMasterH> getDLawCaseMasterHs() {
        return this.DLawCaseMasterHs;
    }

    public void setDLawCaseMasterHs(Set<DLawCaseMasterH> DLawCaseMasterHs) {
        this.DLawCaseMasterHs = DLawCaseMasterHs;
    }
}

DLawCaseMaster.hbm.xml

<set inverse="true" name="DLawCaseMasterHs">
  <key>
    <column name="CASE_ID" not-null="true"/>
  </key>
  <one-to-many class="com.law.beans.DLawCaseMasterH"/>
</set>

DLawCaseMasterH.java

public class DLawCaseMasterH  implements java.io.Serializable {
    private DLawCaseMasterHId id;
    private DLawCaseMaster DLawCaseMaster;

    public void setId(DLawCaseMasterHId id) {
        this.id = id;
    }
    public DLawCaseMaster getDLawCaseMaster() {
        return this.DLawCaseMaster;
    }

    public DLawCaseMaster getDLawCaseMaster() {
        return this.DLawCaseMaster;
    }

    public void setDLawCaseMaster(DLawCaseMaster DLawCaseMaster) {
        this.DLawCaseMaster = DLawCaseMaster;
    }
}

DLawCaseMasterH.hbm.xml

<composite-id class="com.law.beans.DLawCaseMasterHId" name="id">
  <key-property name="historyId" type="long">
    <column name="HISTORY_ID"/>
  </key-property>
  <key-property name="caseId" type="long">
    <column name="CASE_ID"/>
  </key-property>
</composite-id>
<many-to-one class="com.law.beans.DLawCaseMaster" fetch="select" insert="false" name="DLawCaseMaster" update="false">
  <column name="CASE_ID" not-null="true"/>
</many-to-one>

我尝试了以下策略,但每次都返回空白列表。

public static List<DLawCaseMaster> getCaseListWithHistory(long userID, Date asOnDate, Date fromDate, Date toDate) {
    List<DLawCaseMaster> list = new ArrayList<DLawCaseMaster>();
    Session session = null;
    Transaction tx = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(DLawCaseMaster.class);
        criteria.add(Restrictions.isNull("disposalDate"));

        if (asOnDate == null) {
            criteria.add(Restrictions.between("disposalDate", fromDate, toDate));
        } else {
            criteria.add(Restrictions.eq("disposalDate", asOnDate));
        }
        criteria.setFetchMode("DLawUserMaster", FetchMode.JOIN).add(Restrictions.eq("DLawUserMaster.userId", userID));
        criteria.setFetchMode("DLawAdvocateMaster", FetchMode.JOIN).add(Restrictions.eq("DLawUserMaster.userId", userID));
        criteria.setFetchMode("DLawCourtMaster", FetchMode.JOIN).add(Restrictions.eq("DLawUserMaster.userId", userID));
        criteria.setFetchMode("DLawClientMaster", FetchMode.JOIN).add(Restrictions.eq("DLawUserMaster.userId", userID));
        criteria.setFetchMode("DLawCaseTypeMaster", FetchMode.JOIN).add(Restrictions.eq("DLawUserMaster.userId", userID));
        criteria.setFetchMode("DLawStageMaster", FetchMode.JOIN).add(Restrictions.eq("DLawUserMaster.userId", userID));
        // PROBLEM LINE HERE
criteria.createCriteria("DLawCaseMasterHs").add(Restrictions.eq("DLawUserMaster.userId", userID));
// PROBLEM LINE HERE

        criteria.addOrder(Order.desc("caseId"));

        list = criteria.list();
        tx.commit();
    } catch (Exception e) {
        System.out.println("Exception Message : " + e.getMessage());
        if (tx != null) {
            tx.rollback();
        }
        return list;
    } finally {
        session.close();
    }
    return list;
}

如果我删除问题专栏,它将返回包含3条记录的列表,但是当访问DLawCaseMasterHs时会抛出异常。简而言之,它没有任何数据。

任何人都可以指导我在取父母的同时取小孩......提前谢谢。

1 个答案:

答案 0 :(得分:1)

你有没有试过这个?

criteria.createAlias("DLawCaseMasterHs", "dlawCase", Criteria.LEFT_JOIN);
criteria.add(Restrictions.like("dlawCase.userId", userID));