2009-01-07 87 views
12

考慮以下Hibernate映射文件:應用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」表,即使我已將過濾器屬性添加到剪輯包中。

我在做什麼錯?

回答

16

想通了。對於有興趣的人,我有我的錯地方<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>