考虑以下Hibernate映射文件:
<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>
当我运行以下命令时:
_session.EnableFilter("effectiveDate").SetParameter("asOfDate", DateTime.Today); IList<ContentPackage> items = _session.CreateCriteria(typeof(ContentPackage)) .Add(Restrictions.Eq("Id", id)) .List<ContentPackage>();
生成的SQL在中间映射表(contentAudVidLinks)上具有WHERE子句,而不是在“ Clips”表上,即使我已将filter属性添加到Clip Bags中。
我究竟做错了什么?
弄清楚了。对于任何有兴趣的人,我的<filter>属性在错误的位置:
<filter>
之前:
<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>
后:
<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>