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?