customerクラスとcustomerAddressクラスがあります。CustomerAddressクラスには国nの状態オブジェクトがあります。customerAddressもロードする場合、customerAddressにはcontryNameとcountryIDのみをロードする必要があります。その他の詳細はnullである必要があります。
要するに、CriteriaAPTによって生成したい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));
しかし、これは私にエラーを与えます。どうすればこれを行うことができますか。
CriteriaAPIを使用して指定された列を取得する方法。
@Firoによるガイダンスに従って編集
.Add(Restrictions.Eq("Customer.EstablishmentId", CustomerId))
以前に移動したSetProjection
ので、コードは現在
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>();
これは正常に実行されますが、エラーは発生しませんが、オブジェクトを探すと、customer
そのすべてのプロパティはnull
です。