0

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です。

4

2 に答える 2

2

AliasToBean が正しく機能するには、エイリアスを明示的に指定する必要があります

.SetProjection(Projections.ProjectionList()
    .Add(Projections.Property("Country.CountryID"), "CountryID")
    .Add(Projections.Property("Country.CountryName"), "CountryName"))
    .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Country)));
于 2012-10-26T12:37:57.397 に答える
0

Transformerを使用してこれを行うことはおそらく可能ですが、行う価値がない場合があります。一つには、メソッドで直接インスタンスを作成する方が明確な場合があります。Criteriaインスタンスで複数のProjectionsを含むProjectionsListを指定すると、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);
    }

AliasToBeanResultTransformer(MyDTO.class)がMyDTOのインスタンス化に失敗するも参照してください。

于 2012-11-06T12:28:47.407 に答える