2012-10-25 37 views
0

我有客戶和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

+0

移動'。新增(Restrictions.Eq( 「Customer.EstablishmentId」,CustomerId))''爲了setprojection – Firo

+0

@Firo我做了chnages,並沒有發生錯誤,但它給我的客戶對象的所有屬性「null」。 – Amogh

回答

2

爲AliasToBean正常工作,你需要明確指定別名

.SetProjection(Projections.ProjectionList() 
    .Add(Projections.Property("Country.CountryID"), "CountryID") 
    .Add(Projections.Property("Country.CountryName"), "CountryName")) 
    .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Country))); 
0

雖然可以推測不使用變壓器這一點,它可能不值得做的事情。一方面,直接在方法中構建實例可能會更清晰。如果您指定與您的標準實例的多個突起部分A ProjectionsList則Hibernate會帶回對象的結果列表[]所以...

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) fails to instantiate MyDTO