Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate: how to fetch child collection at the time of parent
    primarykey
    data
    text
    <p>I have casemaster(DLawCaseMaster - parent) class and casemaster history (DLawCaseMasterH - child) class. I have situation where I need parent and child collection both.</p> <p>DLawCaseMaster.java</p> <pre><code>public class DLawCaseMaster implements java.io.Serializable { //..... private Set&lt;DLawCaseMasterH&gt; DLawCaseMasterHs = new HashSet&lt;DLawCaseMasterH&gt;(0); //..... public Set&lt;DLawCaseMasterH&gt; getDLawCaseMasterHs() { return this.DLawCaseMasterHs; } public void setDLawCaseMasterHs(Set&lt;DLawCaseMasterH&gt; DLawCaseMasterHs) { this.DLawCaseMasterHs = DLawCaseMasterHs; } } </code></pre> <p>DLawCaseMaster.hbm.xml</p> <pre><code>&lt;set inverse="true" name="DLawCaseMasterHs"&gt; &lt;key&gt; &lt;column name="CASE_ID" not-null="true"/&gt; &lt;/key&gt; &lt;one-to-many class="com.law.beans.DLawCaseMasterH"/&gt; &lt;/set&gt; </code></pre> <p>DLawCaseMasterH.java</p> <pre><code>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; } } </code></pre> <p>DLawCaseMasterH.hbm.xml</p> <pre><code>&lt;composite-id class="com.law.beans.DLawCaseMasterHId" name="id"&gt; &lt;key-property name="historyId" type="long"&gt; &lt;column name="HISTORY_ID"/&gt; &lt;/key-property&gt; &lt;key-property name="caseId" type="long"&gt; &lt;column name="CASE_ID"/&gt; &lt;/key-property&gt; &lt;/composite-id&gt; &lt;many-to-one class="com.law.beans.DLawCaseMaster" fetch="select" insert="false" name="DLawCaseMaster" update="false"&gt; &lt;column name="CASE_ID" not-null="true"/&gt; &lt;/many-to-one&gt; </code></pre> <p>I have tried following strategy but each time it returns blank list.</p> <pre><code>public static List&lt;DLawCaseMaster&gt; getCaseListWithHistory(long userID, Date asOnDate, Date fromDate, Date toDate) { List&lt;DLawCaseMaster&gt; list = new ArrayList&lt;DLawCaseMaster&gt;(); 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; } </code></pre> <p>If I remove <strong>PROBLEM LINE HERE</strong> it will return list with 3 records but when access DLawCaseMasterHs it throws an exception. In short it does not have any data.</p> <p>Can any one guide me to fetch children while fetching parent... Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload