我有以下映射文件:
<?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
我做错了什么?有更好的方法吗?