考虑以下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”表,即使我已将过滤器属性添加到Bag of Clips。
我做错了什么?
答案 0 :(得分:17)
想出来。对于任何有兴趣的人,我的<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>