views:

3755

answers:

1

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?

+7  A: 

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>
David P