Applying the Hibernate filter attribute to a Bag with a many-to-many relationship
- by David P
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?