I have two tables (parent/child relationship) where I am retrieving all the SimpleOrders for the SimpleHeaders that have a specific value in the medicalRecordNumber field.
The list I am retrieving has the correct number of items. When I try to do a Projections.rowCount() for the same list, I'm getting an incorrect value. It seems to be ignoring the Criteria.DISTINCT_ROOT_ENTITY part of the query.
Am I coding this incorrectly or is there a problem within Hibernate?
Any assistance would be greatly appreciated. Thanks
Hibernate version:
3.0.5
Mapping documents:
Code:
<class name="com.fhcp.laboratory.SimpleHeader" table="LABHDR" mutable="false">
<composite-id name="key" class="com.fhcp.laboratory.LaboratoryHeaderKey">
<key-property name="labTestDate" column="HDDATE"/>
<key-property name="seqNo" column="HDKEYNBR"/>
</composite-id>
<property name="medicalRecordNumber" column="PIEMPNO" type="java.lang.String"/>
<property name="orderingProvider" column="ORORDPRV" type="java.lang.String"/>
<set name="obrs" inverse="true" fetch="join">
<key>
<column name="obdate"/>
<column name="obkeynbr"/>
</key>
<one-to-many class="com.fhcp.laboratory.SimpleOrder"/>
</set>
</class>
<class name="com.fhcp.laboratory.SimpleOrder" table="LABOBR" mutable="false">
<composite-id name="key" class="com.fhcp.laboratory.LaboratoryOrderKey">
<key-property name="labTestDate" column="OBDATE"/>
<key-property name="seqNo" column="OBKEYNBR"/>
<key-property name="resultsProcessingSeqNo" column="OBSEQ"/>
</composite-id>
<property name="batteryID" column="OBBATTID" type="java.lang.String"/>
<property name="batteryDesc" column="OBBATTTXT" type="java.lang.String"/>
<many-to-one
name="header"
class="com.fhcp.laboratory.SimpleHeader"
fetch="join">
<column name="obdate" />
<column name="obkeynbr" />
</many-to-one>
<set name="obxs" inverse="true" fetch="join">
<key>
<column name="oxdate"/>
<column name="oxkeynbr"/>
<column name="oxobrseq"/>
</key>
<one-to-many class="com.fhcp.laboratory.SimpleResult"/>
</set>
</class>
<class name="com.fhcp.laboratory.SimpleResult" table="LABOBX" mutable="false">
<composite-id name="key" class="com.fhcp.laboratory.LaboratoryResultKey">
<key-property name="labTestDate" column="OXDATE"/>
<key-property name="seqNo" column="OXKEYNBR"/>
<key-property name="obrSeqNo" column="OXOBRSEQ"/>
<key-property name="obxSeqNo" column="OXSEQ"/>
</composite-id>
<property name="observationID" column="OXOBID" type="java.lang.String"/>
<property name="observationIDTxt" column="OXOBIDTXT" type="java.lang.String"/>
<component name="result" class="com.fhcp.laboratory.component.TestResult">
<property name="actual" column="OXRESULT" type="java.lang.String"/>
<property name="unitsOfMeasure" column="OXUOM" type="java.lang.String"/>
<property name="abnormal" column="OXABNFLG" type="java.lang.String"/>
<property name="normals" column="OXREFRNG" type="java.lang.String"/>
</component>
<many-to-one
name="obr"
class="com.fhcp.laboratory.SimpleOrder" >
<column name="oxdate" />
<column name="oxkeynbr" />
<column name="oxobrseq"/>
</many-to-one>
</class>
Code between sessionFactory.openSession() and session.close():Code:
System.out.println("Before listCount");
Integer i = (Integer)sess.createCriteria(SimpleOrder.class)
.setProjection(Projections.rowCount())
.createCriteria("header")
.add(Restrictions.eq("medicalRecordNumber",member.getMedicalRecordNo()))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.uniqueResult();
System.out.println("Count of items: " + i);
System.out.println("Before list");
List l = sess.createCriteria(SimpleOrder.class)
.addOrder(Order.desc("key.labTestDate"))
.addOrder(Order.asc("batteryDesc"))
.createCriteria("header")
.add(Restrictions.eq("medicalRecordNumber",member.getMedicalRecordNo()))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
System.out.println("Items in list: " + l.size());
Name and version of the database you are using:
DB2/400 V5R3
The generated SQL (show_sql=true):
Before listCount
Hibernate: select count(*) as y0_ from LABOBR this_ inner join LABHDR simplehead1_ on this_.obdate=simplehead1_.HDDATE and this_.obkeynbr=simplehead1_.HDKEYNBR left outer join LABOBR obrs4_ on simplehead1_.HDDATE=obrs4_.obdate and simplehead1_.HDKEYNBR=obrs4_.obkeynbr left outer join LABOBX obxs5_ on obrs4_.OBDATE=obxs5_.oxdate and obrs4_.OBKEYNBR=obxs5_.oxkeynbr and obrs4_.OBSEQ=obxs5_.oxobrseq where simplehead1_.PIEMPNO=?
Count of items: 2940
Before list
Hibernate: select this_.OBDATE as OBDATE3_, this_.OBKEYNBR as OBKEYNBR3_, this_.OBSEQ as OBSEQ3_, this_.OBBATTID as OBBATTID5_3_, this_.OBBATTTXT as OBBATTTXT5_3_, this_.obdate as obdate5_3_, this_.obkeynbr as obkeynbr5_3_, simplehead1_.HDDATE as HDDATE0_, simplehead1_.HDKEYNBR as HDKEYNBR0_, simplehead1_.PIEMPNO as PIEMPNO4_0_, simplehead1_.ORORDPRV as ORORDPRV4_0_, obrs4_.obdate as obdate5_, obrs4_.obkeynbr as obkeynbr5_, obrs4_.OBDATE as OBDATE5_, obrs4_.OBKEYNBR as OBKEYNBR5_, obrs4_.OBSEQ as OBSEQ5_, obrs4_.OBDATE as OBDATE1_, obrs4_.OBKEYNBR as OBKEYNBR1_, obrs4_.OBSEQ as OBSEQ1_, obrs4_.OBBATTID as OBBATTID5_1_, obrs4_.OBBATTTXT as OBBATTTXT5_1_, obrs4_.obdate as obdate5_1_, obrs4_.obkeynbr as obkeynbr5_1_, obxs5_.oxdate as oxdate6_, obxs5_.oxkeynbr as oxkeynbr6_, obxs5_.oxobrseq as oxobrseq6_, obxs5_.OXDATE as OXDATE6_, obxs5_.OXKEYNBR as OXKEYNBR6_, obxs5_.OXOBRSEQ as OXOBRSEQ6_, obxs5_.OXSEQ as OXSEQ6_, obxs5_.OXDATE as OXDATE2_, obxs5_.OXKEYNBR as OXKEYNBR2_, obxs5_.OXOBRSEQ as OXOBRSEQ2_, obxs5_.OXSEQ as OXSEQ2_, obxs5_.OXOBID as OXOBID6_2_, obxs5_.OXOBIDTXT as OXOBIDTXT6_2_, obxs5_.OXRESULT as OXRESULT6_2_, obxs5_.OXUOM as OXUOM6_2_, obxs5_.OXABNFLG as OXABNFLG6_2_, obxs5_.OXREFRNG as OXREFRNG6_2_, obxs5_.oxdate as oxdate6_2_, obxs5_.oxkeynbr as oxkeynbr6_2_, obxs5_.oxobrseq as oxobrseq6_2_ from LABOBR this_ inner join LABHDR simplehead1_ on this_.obdate=simplehead1_.HDDATE and this_.obkeynbr=simplehead1_.HDKEYNBR left outer join LABOBR obrs4_ on simplehead1_.HDDATE=obrs4_.obdate and simplehead1_.HDKEYNBR=obrs4_.obkeynbr left outer join LABOBX obxs5_ on obrs4_.OBDATE=obxs5_.oxdate and obrs4_.OBKEYNBR=obxs5_.oxkeynbr and obrs4_.OBSEQ=obxs5_.oxobrseq where simplehead1_.PIEMPNO=? order by this_.OBDATE desc, this_.OBBATTTXT asc
Items in list: 116