NHibernate:过滤包含连接的子集合

发布于 2024-09-04 04:43:50 字数 2544 浏览 6 评论 0原文

我有以下映射文件:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyAssembly" namespace="EntityNamespace">
  <class name="Server" xmlns="urn:nhibernate-mapping-2.2" table="tblServer" >
    <id name="Id" type="int">
      <generator class="native" />
    </id>
    <bag name="Services" cascade="all" inverse="true" lazy="true">
           <key column="Id" />
      <one-to-many class="Service" />
      <filter name="currentServices" condition=":currentDate BETWEEN DtStart AND DtEnd" />
    </bag>

  </class>

  <filter-def name="currentServices">
    <filter-param name="currentDate" type="System.DateTime"/>
  </filter-def>
</hibernate-mapping>

和:

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" namespace="EntityNamespace">
  <class name="Service" xmlns="urn:nhibernate-mapping-2.2" table="tblService" >
    <id name="Id" type="int">
      <generator class="native" />
    </id>
    <many-to-one name="Server" column="server_id" />

    <join table="tblServiceOptMatrix">
      <key column="ServiceOptID" />
      <property name="StartDate" column="DtStart" type="DateTime" />
      <property name="EndDate" column="DtEnd" type="DateTime" />
    </join>
  </class>
</hibernate-mapping>

以及以下条件 API:

IList<Server> servers;
using (var tx = Session.BeginTransaction())
{
    Session.EnableFilter("currentServices")
        .SetParameter("currentDate", DateTime.Now);
    servers = Session.CreateCriteria<Server>()
        .List<Server>();
    tx.Commit();
}

以及为延迟检索服务生成的 sql 如下所示:

SELECT service0_.server_id as server2_1_,
       service0_.id        as id1_,
       service0_1_.DtStart as DtStart256_0_,
       service0_1_.DtEnd   as DtEnd256_0_
FROM   tblTfopt valueadded0_
       inner join tblServiceOptMatrix service0_1_
         on service0_.id = service0_1_.ServiceOptID
WHERE  '2010-01-07T14:58:09.00' /* @p0 */ 
    BETWEEN service0_.DtStart AND service0_.DtEnd
       and service0_.server_id = 993 /* @p1 */

其中服务器 ID 和日期是示例。问题是为过滤器生成的 sql 使用了错误的表别名,它应该是这样的:

BETWEEN service0_1_.DtStart AND service0_1_.DtEnd

我做错了什么?有更好的方法吗?

I have the following mapping files:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyAssembly" namespace="EntityNamespace">
  <class name="Server" xmlns="urn:nhibernate-mapping-2.2" table="tblServer" >
    <id name="Id" type="int">
      <generator class="native" />
    </id>
    <bag name="Services" cascade="all" inverse="true" lazy="true">
           <key column="Id" />
      <one-to-many class="Service" />
      <filter name="currentServices" condition=":currentDate BETWEEN DtStart AND DtEnd" />
    </bag>

  </class>

  <filter-def name="currentServices">
    <filter-param name="currentDate" type="System.DateTime"/>
  </filter-def>
</hibernate-mapping>

and:

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" namespace="EntityNamespace">
  <class name="Service" xmlns="urn:nhibernate-mapping-2.2" table="tblService" >
    <id name="Id" type="int">
      <generator class="native" />
    </id>
    <many-to-one name="Server" column="server_id" />

    <join table="tblServiceOptMatrix">
      <key column="ServiceOptID" />
      <property name="StartDate" column="DtStart" type="DateTime" />
      <property name="EndDate" column="DtEnd" type="DateTime" />
    </join>
  </class>
</hibernate-mapping>

and the following criteria API:

IList<Server> servers;
using (var tx = Session.BeginTransaction())
{
    Session.EnableFilter("currentServices")
        .SetParameter("currentDate", DateTime.Now);
    servers = Session.CreateCriteria<Server>()
        .List<Server>();
    tx.Commit();
}

and the sql that is generated for lazily retrieving services looks like this:

SELECT service0_.server_id as server2_1_,
       service0_.id        as id1_,
       service0_1_.DtStart as DtStart256_0_,
       service0_1_.DtEnd   as DtEnd256_0_
FROM   tblTfopt valueadded0_
       inner join tblServiceOptMatrix service0_1_
         on service0_.id = service0_1_.ServiceOptID
WHERE  '2010-01-07T14:58:09.00' /* @p0 */ 
    BETWEEN service0_.DtStart AND service0_.DtEnd
       and service0_.server_id = 993 /* @p1 */

where the server id and date are examples. The problem is that the sql produced for the filter is using the wrong table alias, It should instead be this:

BETWEEN service0_1_.DtStart AND service0_1_.DtEnd

What am I doing wrong? Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文