我有客戶和customerAddress類。 CustomerAddress類有國家n狀態對象。當我加載客戶時,我也想要CustomerAddress,並且在customerAddress中,我想只加載contryName和countryID,其他詳細信息必須爲空。使用Criteria API獲取所需屬性?
中,我想通過標準APT到genrate短SQL查詢
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));
,但是這給了我的錯誤。如何做到這一點。
如何使用Criteria API獲取指定的列。
市價修改指導做好通過@Firo
我SetProjection
之前移動.Add(Restrictions.Eq("Customer.EstablishmentId", 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>();
這將成功執行沒有錯誤會occure但是當我去找customer
對象其所有財產是null
。
移動'。新增(Restrictions.Eq( 「Customer.EstablishmentId」,CustomerId))''爲了setprojection – Firo
@Firo我做了chnages,並沒有發生錯誤,但它給我的客戶對象的所有屬性「null」。 – Amogh