AddQueries方法將按如下實現,以「修復」Fluent NHibernate缺乏Loader支持。訣竅是正確設置INativeSQLQueryReturn []值以包含從表列到實體屬性的映射。它應該模擬HBM文件中返回sql-query元素的內容,其中定義了類(具有名稱空間)和屬性映射(請參見下面的XML)。感謝@ jimbobmcgee讓我開始朝這個方向發展!
private static void AddQueries(Configuration cfg)
{
var namedQuery = new NamedSQLQueryDefinition(
"exec dbo.pr_GETCustomers @CustomerID=?",
new INativeSQLQueryReturn[]
{
new NativeSQLQueryRootReturn(
"Customers",
"VehicleInfo.Entities.Customers",
new Dictionary<string, string[]>
{
{"CustomerID", new[] {"CustomerID"}},
{"CompanyName", new[] {"CompanyName"}}
},
LockMode.Read)
},
new List<string> { "dbo.Customers" },
true,
null,
15,
1000,
FlushMode.Auto,
CacheMode.Normal,
false,
"",
null,
true);
cfg.NamedSQLQueries.Add("pr_GETCustomers", namedQuery);
var cust = cfg.GetClassMapping(typeof(Customers));
cust.LoaderName = "pr_GETCustomers";
}
樣品HBM文件做同樣的事情:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
default-access="property" auto-import="true"
default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2"
mutable="true" name="VehicleInfo.Entities.Customers, VehicleInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Customers">
<id name="CustomerID" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="CustomerID" />
<generator class="assigned" />
</id>
<property name="CompanyName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="CompanyName" />
</property>
<loader query-ref="pr_GETCustomers"/>
<sql-insert callable="true" check="none">exec dbo.pr_INSERTCustomers @CompanyName=?, @CustomerID=?</sql-insert>
<sql-update callable="true" check="none">exec dbo.pr_UPDATECustomers @CompanyName=?, @CustomerID=?</sql-update>
<sql-delete callable="true" check="none">exec dbo.pr_DELETECustomers @CustomerID=?</sql-delete>
</class>
<sql-query name="pr_GETCustomers">
<return alias="cust" class="VehicleInfo.Entities.Customers, VehicleInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<return-property name="CustomerID" column="CustomerID"></return-property>
<return-property name="CompanyName" column="CompanyName"></return-property>
</return>
exec dbo.pr_GETCustomers @CustomerID=?
</sql-query>
</hibernate-mapping>
謝謝,我會嘗試這種方法。我還試圖弄清楚QuerySpaces是什麼,並且不想挖掘NHibernate源代碼。 – 2010-08-10 13:13:36