使用Criteria API获取所需的属性?

时间:2012-10-25 12:50:31

标签: nhibernate c#-4.0 criteria-api

我有customer和customerAddress类。 CustomerAddress类具有country n state对象。当我加载customer时我也想要CustomerAddress,而在customerAddress中我只想加载contryName和countryID,其他细节必须为null。

简而言之,我想通过Criteria APT生成的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

我在.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

2 个答案:

答案 0 :(得分:2)

要使AliasToBean正常工作,您需要显式指定别名

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

答案 1 :(得分: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) fails to instantiate MyDTO