将Hibernate过滤器属性应用于具有多对多关系的Bag [英] Applying the Hibernate filter attribute to a Bag with a many-to-many relationship

查看:98
本文介绍了将Hibernate过滤器属性应用于具有多对多关系的Bag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的Hibernate映射文件:

 < hibernate-mapping ...> 
< class name =ContentPackagetable =contentPackages>
< id name =Idcolumn =idtype =int>< generator class =native/>< / id>
...
< bag name =Clipstable =contentAudVidLinks>
< key column =fk_contentPackageId>< / key>
< filter name =effectiveDatecondition =: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> 属性都在错误的位置:



之前:

 < bag name =剪辑table =ctv_bb_contentAudVidLinks> 
< key column =fk_contentPackageId>< / key>
< filter name =effectiveDatecondition =:asOfDate BETWEEN startDate and endDate/>
< / bag>

后:

 < bag name =剪辑table =ctv_bb_contentAudVidLinks> 
< key column =fk_contentPackageId>< / key>
< filter name =effectiveDatecondition =:asOfDate BETWEEN startDate and endDate/>
< /多对多>
< / bag>


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?

解决方案

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>

这篇关于将Hibernate过滤器属性应用于具有多对多关系的Bag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆