Hibernate 3.1.3
We are using XDoclet 2 for generating our hibernate mappings.
We have an issue where the generated SQL from the HQL is not correct.
We have 2 tables and these are the mappings (parent first and then child)
, we are having bidrectional association between these two.
Parent Mapping
-----------------
<hibernate-mapping>
<class table="T_CLAIMANT" name="ClaimantData">
<id column="CLAIMANT_ID" name="pkId">
<generator class="sequence">
<param name="sequence">SEQ_T_CLAIMANT</param>
</generator>
</id>
<set lazy="true" inverse="true" cascade="none" name="claimData">
<key column="CLAIMANT_ID"/>
<one-to-many class="gov.ms.mdes.access.domain.data.ClaimData"/>
</set>
<property name="ssn" not-null="true" column="SSN"/>
<property name="dateOfBirth" not-null="true" column="DATE_OF_BIRTH"/>
<property name="firstName" not-null="true" column="FIRST_NAME"/>
<property name="lastName" not-null="true" column="LAST_NAME"/>
<property name="lastNameSSCard" not-null="false" column="LAST_NAME_SS_CARD"/>
<property name="middleInitial" not-null="false" column="MIDDLE_INITIAL"/>
</class>
<query name="fetchDataWorking"><![CDATA[select claimData from ClaimData claimData where claimData.claimantData.ssn = :ssn and claimData.status = 'ACTV' order by claimData.byeDate desc]]></query>
<query name="fetchDataNotWorking"><![CDATA[select claimantData.claimData from ClaimantData claimantData where claimantData.ssn = :ssn and claimantData.claimData.status = 'ACTV' order by xyz.claimData.byeDate desc]]></query>
</hibernate-mapping>
-----------------------------------------------
Child Mapping
--------------------------------------------------
<hibernate-mapping>
<class table="T_CLAIM" name="ClaimData">
<id column="CLAIM_ID" name="claimId">
<generator class="sequence">
<param name="sequence">SEQ_T_CLAIM</param>
</generator>
</id>
<property name="status" not-null="false" column="STATUS"/>
<property name="monEligibleFlag" not-null="false" column="MON_ELIGIBLE_FLAG"/>
<property name="naics" not-null="false" column="NAICS"/>
<property name="residenceCounty" not-null="false" column="RESIDENCE_COUNTY_ID"/>
<property name="byeDate" not-null="false" type="date" column="BYE_DATE"/>
<many-to-one not-null="true" column="CLAIMANT_ID" name="claimantData" class="ClaimantData"/>
</hibernate-mapping>
-------------------------------------------------------------------------
I have placed 2 queries in the parent mapping, waht ia m trying to do is to fetch the child object based on the property of the parent.
The query named "fetchDataNotWorking" is generating the wrong sql, which we had initially and then we changed it to "fetchDataWorking" and then it started working fine.
This is the query which it generates for the one which is not working as it is joining the same table more than once for every condition in the hql query.
---------------Generated SQL----------------------
select claimdata1_.CLAIM_ID as CLAIM1_56_, claimdata1_.BYE_DATE as BYE3_56_, <<other different columns of the same table>>
from T_CLAIMANT claimantda0_ inner join T_CLAIM claimdata1_ on claimantda0_.CLAIMANT_ID=claimdata1_.CLAIMANT_ID, T_CLAIM claimdata2_, T_CLAIM claimdata3_
where claimantda0_.CLAIMANT_ID=claimdata3_.CLAIMANT_ID and claimantda0_.CLAIMANT_ID=claimdata2_.CLAIMANT_ID and claimantda0_.SSN=? and claimdata2_.STATUS='ACTV'
order by claimdata3_.BYE_DATE desc
The first inner join is fine, but then we have same child table "T_CLAIM" used in the from clause more than once, which in turn returns more # of records than expected.
Any hint that what might be going wrong.
Thanks
|