ケースマスター(DLawCaseMaster - 親) クラスとケースマスター履歴 (DLawCaseMasterH - 子) クラスがあります。親コレクションと子コレクションの両方が必要な状況があります。
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;
}
PROBLEM LINE HEREを削除すると、3 つのレコードを含むリストが返されますが、DLawCaseMasterHs にアクセスすると例外がスローされます。要するに、データがありません。
親をフェッチしながら子供をフェッチするように私を導くことができますか...前もって感謝します。