I have a question how to use hibernate filters with map-collections:
Where do i put the filter-element in my mapping-file for filtering keys of the map of a many-to-many relation?
I try to explain it with an example:
Given an Item-class that has a many-to-many-relation to a Resource-class. The resources have a language-property. When i access Items from the database sometimes i only need the subset of the resources, that match a language-criteria. Since i want to keep my dao-implementation independent of the the knowledge, whether a language restriction exists or not, i choose to use hibernate filters for this:
Code:
<class name="Item">
<id name="Id" column="item_id" type="long"/>
<set name="Resources" table="item_resource">
<key column="item_id" />
<many-to-many class="Resource" column="resource_id">
<filter name="languageFilter" condition="language_id in ( :languages )"/>
</many-to-many>
</set>
<class>
So far, everythis is perfect: When i activate the filter, hibernate only fetches the Resource-Entries for the "Resouces"-Set that match the criteria.
But now i have to put an extra propertry "usage" to the join-table item_resource that contains information about for what the item needs the resource. To receive this information, i switch the set Resources to a map, that contains the Resource as key and the usage-porperty as value:
Code:
<class name="Item">
<id name="Id" column="item_id" type="long"/>
<map name="Resources" table="item_resource">
<key column="item_id" />
<map-key-many-to-many class="Resource" column="resource_id">
</map-key-many-to-many>
<element column="usage" type="string">
</map >
<class>
But where to i put my filter-element? map-key-many-to-many doesn't allow a filter-subelement. And a filter in the map-element accesses properteis in the item_resource-table, not in the resources-table.
My only idea is:
Code:
<filter name="languageFilter" condition="exits( select resource_id from resource r where r.resource_id = resource_id and language_id in ( :languages ))"/>
But i think this will have worse performance than the join hibernate generates with the Set-solution.