我有这堂课:
public Customer
{
public int CustomerId {get;set;}
....
public List<Order> OrderList {get;set;}
}
我想使用NHibernate CreateSQLQuery
编写这个SQL查询SELECT C.CustomerId, .... O.OrderId, O.OrderDate, O.OrderTotal.....
FROM Customers C
LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID
如何进行此转换:
.SetResultTransformer(Transformers.AliasToBean<Customer>())
它是否包含每个Customer对象中的Orders列表?
由于
答案 0 :(得分:0)
假设结果的所有列名称都映射到实体的属性名称,您可以尝试:
var myQuery = @"SELECT C.CustomerId, .... O.OrderId, O.OrderDate, O.OrderTotal.....
FROM Customers C
LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID";
myNHSession.CreateSQLQuery(myQuery)
.AddEntity("C",typeof(Customer))
.AddJoin("O","C.OrderList")
.List();
希望这会有所帮助
答案 1 :(得分:0)
如果可以,请使用命名查询。
<sql-query name="myQuery">
<return alias="c" class="Customer" />
<return-join alias="o" property="c.Orders" />
<![CDATA[
SELECT {c.*}, {o.*} FROM Customers c LEFT OUTER JOIN Orders o ON c.CustomerId = o.CustomerId
]]>
</sql-query>
NHibernate理解{c.*}
和{o.*}
,并在会话构建时将其替换为实际列。另一个优点是您可以在应用程序启动时预编译这些查询。
var results = session.GetNamedQuery("myQuery").List<Customer>();