Hello,
I would like to use custom SQL for loading a collection. After a look to the Hibernate reference I need to say that this feature is quite poorly documented, at least for me (I am a newbe). Examples presented there do not show how to use this in general. A also checked "Hibernate in Action" and "Hibernate Quickly" and found no answer.
My mappings are as follows:
Code:
<hibernate-mapping>
<class name="GeographicLocationGroup" table="LOCATIONGROUP" lazy="false" >
<composite-id name="id" class="GeographicLocationGroupId">
<key-property name="label" column="LABEL"/>
<key-property name="pseudoCity" column="PSEUDOCITY"/>
</composite-id>
<set name="includedLocations" cascade="persist,delete-orphan" lazy="false" inverse="true">
<key>
<column name="LABEL" not-null="true"></column>
<column name="PSEUDOCITY" not-null="true"></column>
</key>
<one-to-many class="GeographicLocation"/>
<loader query-ref="includedLocationsSql"/>
</set>
<set name="excludedLocations" cascade="persist,delete-orphan" lazy="false" inverse="true" >
<key>
<column name="LABEL" not-null="true"></column>
<column name="PSEUDOCITY" not-null="true"></column>
</key>
<one-to-many class="GeographicLocation"/>
<loader query-ref="excludedLocationsSql"/>
</set>
</class>
<sql-query name="includedLocationsSql" >
<load-collection alias="location" role="GeographicLocationGroup.includedLocations"/>
SELECT {location.*}
FROM LOCATIONGROUPLIST location
WHERE INCLEXCL = 'I'
</sql-query>
<sql-query name="excludedLocationsSql">
<load-collection alias="location" role="GeographicLocationGroup.excludedLocations"/>
SELECT {location.*}
FROM LOCATIONGROUPLIST location
WHERE INCLEXCL = 'E'
</sql-query>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="GeographicLocation" table="LOCATIONGROUPLIST" lazy="false">
<composite-id name="id" class="GeographicLocationId" >
<key-property name="label" column="LABEL"/>
<key-property name="pseudoCity" column="PSEUDOCITY"/>
</composite-id>
<property name="inclExcl" column="INCLEXCL" not-null="true"/>
</class>
</hibernate-mapping>
In fact they contain more properties but I didn't include them to make the thing shorter.
And now the question: the only problem is how to reference the key defined in <set>. I guess that it's a question about how does these key's values map to named parameters in the sql query so that they could be referenced in the 'WHERE' clause.
A hope the question is clear enough:).
Thanks for any help:).