damonrand wrote:
Hi there,
I am pretty new to Hibernate so I may be going about the below completely the wrong way..
I have an Asset class that has two many-to-many relationships to a Keyword class as follows..
<set name="concernKeywords" table="asset_keyword">
<key column="asset_id" />
<many-to-many class="ConcernKeyword" column="keyword_id"/>
</set>
<set name="countryKeywords" table="asset_keyword">
<key column="asset_id" />
<many-to-many class="CountryKeyword" column="keyword_id"/>
</set>
And the Keyword class.
<class name="Keyword" table="keyword">
<id name="keywordId" column="keyword_id" type="java.lang.Integer">
<generator class="assigned"/>
</id>
<discriminator column="keyword_class" type="java.lang.String"/>
<property name="keyword" column="keyword" type="java.lang.String" not-null="false" />
<property name="keywordStatus" column="keyword_status" type="java.lang.String"
not-null="false" />
<subclass name="ConcernKeyword"
discriminator-value="CONCERN">
</subclass>
<subclass name="CountryKeyword"
discriminator-value="COUNTRY">
</subclass>
</class>
Note that the asset_keyword table is used to represent both many-to-many joins. I would have expected Hibernate to be able to use the discriminator of the Keyword class in the join when it fetches each of the two Set objects. However, this is not what happens. Hibernate loads all related concerns AND countrys into both sets. It does not put a necessary "where keyword.keyword_class='CONCERN'" clause into its query.
Is there a workaround for this or must I break the asset_keyword junction table into two junction tables?
I tried using <set where="keyword_class='CONCERN'" /> but this looks for asset_keyword.keyword_class which doesn't exist.
Regards,
Damon.
You mean this did not work for you ?
Code:
<set name="concernKeywords" table="asset_keyword" [b]where="keyword_class='CONCERN'"[/b]>
<key column="asset_id" />
<many-to-many class="ConcernKeyword" column="keyword_id"/>
</set>
<set name="countryKeywords" table="asset_keyword" [b]where="keyword_class='COUNTRY'"[/b]>
<key column="asset_id" />
<many-to-many class="CountryKeyword" column="keyword_id"/>
</set>