我有 casemaster(DLawCaseMaster - parent) 类和 casemaster history (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 时会引发异常。简而言之,它没有任何数据。
任何人都可以在接父母的同时指导我接孩子...提前谢谢。