Nhibernate 是否使用标准API获取所需的属性?
我有customer和customerAddress类。CustomerAddress类具有country n state对象。加载customer时,我还需要CustomerAddress,而在CustomerAddress中,我只想加载contryName和countryID其他详细信息必须为null 简言之,我想根据条件生成的SQL查询是APTNhibernate 是否使用标准API获取所需的属性?,nhibernate,c#-4.0,criteria-api,Nhibernate,C# 4.0,Criteria Api,我有customer和customerAddress类。CustomerAddress类具有country n state对象。加载customer时,我还需要CustomerAddress,而在CustomerAddress中,我只想加载contryName和countryID其他详细信息必须为null 简言之,我想根据条件生成的SQL查询是APT SELECT Customer.Name, Country.CountryName, Country.Countr
SELECT Customer.Name, Country.CountryName,
Country.CountryID AS CountryID,
CustomerAddress.LocalAddressLine1
FROM Customer INNER JOIN CustomerAddress
ON Customer.CustomerID = CustomerAddress.CustomerID
INNER JOIN Country
ON CustomerAddress.CountryID = Country.CountryID
为了做到这一点,我做到了
ICriteria criteria = session.CreateCriteria(typeof(Customer), "Customer")
.CreateAlias("Customer.CustomerAddressList", "CustomerAddressList", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateCriteria("CustomerAddressList.Country", "Country", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.SetProjection(Projections.ProjectionList().Add(Projections.Property("Country.CountryID"))
.Add(Projections.Property("Country.CountryName")))
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Customer)))
.Add(Restrictions.Eq("Customer.EstablishmentId", CustomerId));
但这给了我错误。我怎么能做到这一点
如何使用标准API获取指定列?
根据@Firo的指南编辑
我在SetProjection
之前移动了.Add(Restrictions.Eq(“Customer.restitmentid”,CustomerId))
,所以我的代码现在是
ICriteria criteria = session.CreateCriteria(typeof(Customer), "Customer")
.CreateAlias("Customer.CustomerAddressList", "CustomerAddressList", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateCriteria("CustomerAddressList.Country", "Country", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("Customer.EstablishmentId", CustomerId))
.SetProjection(Projections.ProjectionList().Add(Projections.Property("Country.CountryID"))
.Add(Projections.Property("Country.CountryName")))
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Customer)));
customer = criteria.UniqueResult<Customer>();
ICriteria标准=session.CreateCriteria(typeof(Customer),“Customer”)
.CreateAlias(“Customer.CustomerAddressList”、“CustomerAddressList”、NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateCriteria(“CustomerAddressList.Country”、“Country”、NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq(“Customer.estimentId”,CustomerId))
.SetProjection(Projections.ProjectionList().Add(Projections.Property(“Country.CountryID”))
.Add(Projections.Property(“Country.CountryName”))
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Customer)));
customer=criteria.UniqueResult();
这将成功执行,不会发生任何错误,但当我查找
客户
对象时,其所有属性都是空
要使AliasToBean正常工作,您需要显式指定别名
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Country.CountryID"), "CountryID")
.Add(Projections.Property("Country.CountryName"), "CountryName"))
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Country)));
虽然您可以使用变压器来实现这一点,但可能不值得这么做。首先,直接在方法中构造实例可能更清晰。如果您在Criteria实例上指定了一个ProjectionList,其中包含多个投影,那么Hibernate将返回Object[]的结果列表,因此
List<Object[]> results = crit.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Country.CountryID"), "CountryID")
.Add(Projections.Property("Country.CountryName"), "CountryName")).list();
List<Customer> customers = new ArrayList<Customer>();
for ( Object[] row: results ) {
Customer c = new Customer();
c.setCountryId((Long) row[0]);
// etc. for other properties
customers.add(c);
}
List results=crit.SetProjection(Projections.ProjectionList())
.Add(Projections.Property(“Country.CountryID”),“CountryID”)
.Add(Projections.Property(“Country.CountryName”),“CountryName”).list();
列出客户=新建ArrayList();
对于(对象[]行:结果){
客户c=新客户();
c、 setCountryId((长)行[0]);
//其他物业等
加上(c);
}
另请参见move
.Add(Restrictions.Eq(“Customer.restitmentid”,CustomerId))
在setprojection@Firo我做了更改,没有发生错误,但它给了我customer对象及其所有属性“null”。