Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying the Hibernate filter attribute to a Bag with a many-to-many relationship

Consider the following Hibernate mapping file:

<hibernate-mapping ...>
<class name="ContentPackage" table="contentPackages">
    <id name="Id" column="id" type="int"><generator class="native" /></id>
    ...
    <bag name="Clips" table="contentAudVidLinks">
      <key column="fk_contentPackageId"></key>
      <many-to-many class="Clip" column="fk_AudVidId"></many-to-many>
      <filter name="effectiveDate" condition=":asOfDate BETWEEN startDate and endDate"  />
    </bag>
</class>
</hibernate-mapping>

When I run the following command:

  _session.EnableFilter("effectiveDate").SetParameter("asOfDate", DateTime.Today);

  IList<ContentPackage> items = _session.CreateCriteria(typeof(ContentPackage))
                                     .Add(Restrictions.Eq("Id", id))
                                     .List<ContentPackage>();

The resulting SQL has the WHERE clause on the intermediate mapping table (contentAudVidLinks), rather than the "Clips" table even though I have added the filter attribute to the Bag of Clips.

What am I doing wrong?

like image 430
David P Avatar asked Jan 07 '09 16:01

David P


1 Answers

Figured it out. For anyone interested, I had my <filter> attribute in the wrong place:

Before:

<bag name="Clips" table="ctv_bb_contentAudVidLinks">
  <key column="fk_contentPackageId"></key>
  <many-to-many class="Clip" column="fk_AudVidId"></many-to-many>
  <filter name="effectiveDate" condition=":asOfDate BETWEEN startDate and endDate" />
</bag>

After:

<bag name="Clips" table="ctv_bb_contentAudVidLinks">
  <key column="fk_contentPackageId"></key>
  <many-to-many class="Clip" column="fk_AudVidId">
    <filter name="effectiveDate" condition=":asOfDate BETWEEN startDate and endDate" />
  </many-to-many>
</bag>
like image 137
David P Avatar answered Sep 30 '22 23:09

David P