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

Posted by David P on Stack Overflow See other posts from Stack Overflow or by David P
Published on 2009-01-07T16:48:20Z Indexed on 2010/04/03 8:03 UTC
Read the original article Hit count: 334

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?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about hibernate