0

我有客户和客户地址类。CustomerAddress 类具有国家/地区 n 状态对象。当我加载客户时,我也想要 CustomerAddress 并且在 customerAddress 中我只想加载 contryName 和 countryID 其他详细信息必须为空。

简而言之,我想通过 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.

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[] So ...

    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 回答