-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using hibernate filters to filter map entries
PostPosted: Tue Feb 14, 2006 6:05 am 
Newbie

Joined: Tue Feb 14, 2006 5:13 am
Posts: 18
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.