NHibernate:过滤包含连接的子集合
我有以下映射文件:
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论