如何使用Hibernate转换平坦的结果集 [英] How to transform a flat result set using Hibernate

查看:149
本文介绍了如何使用Hibernate转换平坦的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将SQL的结果映射到非平面对象?

  List< Customer>客户= hibernateSession()。createCriteria(CustomerDetailsView.class)
.add(Restrictions.in(userName,userName))
.setProjection(buildProjection())
.setResultTransformer(Transformers。 aliasToBean(Customer.class))
.list();

在我的情况中, CustomerDetailsView 具有扁平结构。但我需要将它映射到像这样的对象:

  public class Customer {
private String userName;
私有字符串标题;
private String firstName;
private String lastName;
私有字符串类型;
private String companyName;
private AddressDetails addressDetails;
}

  public class AddressDetails {
private String countryCode;
私人字符串addressLine1;
私人字符串zipOrPostCode;
私人字符串城市;
private String countryDivisionName;
private String countryDivisionCode;
private String countryDivisionTypeCode;
private String residentialAddress;


解决方案

是的,这是可能的。您可以使用自定义转换器:
FluentHibernateResultTransformer


您可以复制粘贴代码,或者通过Maven添加jar:> 标准与预测。请不要忘记指定投影别名( userName addressDetails.countryCode

  Criteria criteria = session.createCriteria(Customer.class); 
criteria.createAlias(addressDetails,addressDetails,JoinType.LEFT_OUTER_JOIN);

criteria.setProjection(Projections.projectionList()
.add(Projections.property(userName)。as(userName))
.add(Projections.property (addressDetails.countryCode)
.as(addressDetails.countryCode)));

列表< Customer> customers = criteria.setResultTransformer(
new FluentHibernateResultTransformer(Customer.class))。list();

与HQL一起使用



由于Hibernate不允许在HQL中使用嵌套别名,因此不可能将它与HQL一起使用。



select addressDetails.countryCode as addressDetails.countryCode



这将是 c $ c> addressDetails.countryCode 别名的错误。



与原生SQL一起使用
$ b 变压器可用于本地SQL与嵌套投影(与HQL相反)。
在这种情况下,需要使用带引号的别名:

  String sql =select c.f_user_name as userName,d.f_country_code as \addressDetails.countryCode\customers 
+from customers c left outer join address_details d on c.fk_details = d.f_pid;

列表< Customer> customers = session.createSQLQuery(sql)
.setResultTransformer(new FluentHibernateResultTransformer(Customer.class))
.list();


Is it possible to map result of SQL to not flat object?

List<Customer> customers = hibernateSession().createCriteria(CustomerDetailsView.class)
                .add(Restrictions.in("userName", userName))
                .setProjection(buildProjection())
                .setResultTransformer(Transformers.aliasToBean(Customer.class))
                .list();

In my case CustomerDetailsView has flat structure. But I need to map it to object like this:

public class Customer {
    private String userName;
    private String title;
    private String firstName;
    private String lastName;
    private String type;
    private String companyName;
    private AddressDetails addressDetails;
}

and

public class AddressDetails {
    private String countryCode;
    private String addressLine1;
    private String zipOrPostCode;
    private String city;
    private String countryDivisionName;
    private String countryDivisionCode;
    private String countryDivisionTypeCode;
    private String residentialAddress;
}

解决方案

Yes it is possible. You can use a custom transformer for it: FluentHibernateResultTransformer.

You can copy paste code, or add the jar by Maven: fluent-hibernate-core.

You need to use Criteria with Projections. Please, don't forget to specify projection aliases (userName, addressDetails.countryCode)

Criteria criteria = session.createCriteria(Customer.class);
criteria.createAlias("addressDetails", "addressDetails", JoinType.LEFT_OUTER_JOIN);

criteria.setProjection(Projections.projectionList()
        .add(Projections.property("userName").as("userName"))
        .add(Projections.property("addressDetails.countryCode")
        .as("addressDetails.countryCode")));

List<Customer> customers = criteria.setResultTransformer(
        new FluentHibernateResultTransformer(Customer.class)).list();

Using with HQL

It is impossible to use it with HQL, because of Hibernate doesn't allow nested aliases in HQL

select addressDetails.countryCode as addressDetails.countryCode

It will be an error with the addressDetails.countryCode alias.

Using with a native SQL

The transformer can be used for a native SQL with the nested projections (opposite HQL). It is need to use the aliases with the quotes in this case:

String sql = "select c.f_user_name as userName, d.f_country_code as \"addressDetails.countryCode\" "
        + "from customers c left outer join address_details d on c.fk_details = d.f_pid";

List<Customer> customers = session.createSQLQuery(sql)
        .setResultTransformer(new FluentHibernateResultTransformer(Customer.class))
        .list();

这篇关于如何使用Hibernate转换平坦的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆