mortenoh
mortenoh

Reputation: 255

Hibernate data filter on many-to-many collection mapping

I have a simple setup where I have

class Translation
  string locale
  enum property (NAME, DESCRIPTION)
  string name

and

class SomeObject
  string name
  string description
  set<Translation> translations;

Which is working fine, translations are mapped as many-to-many using hbm

<set name="translations" table="someobjecttranslations" cascade="delete-orphan" lazy="false">
  <cache usage="read-write" />
  <key column="someobjectid" />
  <many-to-many class="Translation" column="translationid" unique="true" />
</set>

Since the list of translation locales can be potentially huge, wanted to leverage hibernate data filter on this collection, so I added

<filter-def name="locale" condition=" locale = :locale ">
  <filter-param name="locale" type="string" />
</filter-def>

To define the filter, and added to mapping

<set name="translations" table="someobjecttranslations" cascade="delete-orphan" lazy="false">
  <cache usage="read-write" />
  <key column="someobjectid" />
  <many-to-many class="Translation" column="translationid" unique="true" />
  <filter name="locale" />
</set>

I have done something similar before, but not to many-to-many collections, and it has been working fine, but when I do this to a set, it seems that it tries to match the locale against the join table, and not the translation table (which is actually what I want).

Does the data filter actually work for many-to-many collections, and how should I set it up?

Upvotes: 0

Views: 276

Answers (1)

mortenoh
mortenoh

Reputation: 255

Seems Hibernate have two data filter modes, filter, and filter join table.

If used like this

<set name="translations" table="someobjecttranslations" cascade="delete-orphan" lazy="false">
  <cache usage="read-write" />
  <key column="someobjectid" />
  <many-to-many class="Translation" column="translationid" unique="true" />
  <filter name="locale" />
</set>

It filters on the join-table, if however, it's used like this:

<set name="translations" table="someobjecttranslations" cascade="delete-orphan" lazy="false">
  <cache usage="read-write" />
  <key column="someobjectid" />
  <many-to-many class="Translation">
    <column name="translationid" unique="true" />
    <filter name="locale" condition=" locale = :locale "/>
  </many-to-many>
</set>

It does the filter on the inverse side (i.e Translation entity). I had to add the condition here again not sure why, but it seems it couldn't be used with the other filter-def I already had.

Upvotes: 1

Related Questions