将hibernate投影映射到java POJO模型 [英] Map hibernate projections result to java POJO model

查看:144
本文介绍了将hibernate投影映射到java POJO模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周我一直在使用spring和hibernate,并且我一直在那里学习新东西。



现在我遇到了一个问题假设有一个模型 Person ,并且该模型有许多 code>车。以下是类定义的大致形式:

  public class Person implements java.io.Serializable {
私人整数ID;
私人字符串名称;
私人列表< Car>汽车;
私人整数minYear; // Transient
private Integer maxYear; //暂时
}

公共类Car实现java.io.Serializable {
private Integer id;
私人整数年;
}

这里的问题是我想获取 minYear每个个人 maxYear )由最早的年份他们有 cars



后来我找到了一个解决方案,使用 Projections code>但是我偶然发现了 org.hibernate.QueryException:无法解析property:minYear:model.Person ,这里是db操作的代码:

  Criteria criteria = sessionFactory.getCurrentSession()。createCriteria(model.Person); 
criteria.add(create(personInstance));
criteria.createAlias(minYear,minYear);
criteria.setProjection(Projections.min(cars.year)。as(minYear));

有没有办法使用 Projections ,因为我只是希望尽可能避免使用普通的SQL和HQL。

解决方案

没关系,我'b
$ b


  1. 首先,我们需要创建关联对象的别名,比如

      Criteria criteria = sessionFactory.getCurrentSession()。createCriteria(model.Person); 
    criteria.createAlias(cars,cars);


  2. 选择所需的使用Hibernate Projections

      ProjectionList投影= Projections.projectionList(); 
    projections.add(Projections.property(id)。as(id));
    projections.add(Projections.property(name)。as(name));
    projections.add(Projections.property(cars)。as(cars));


  3. 对基于实体的结果进行分组(在这种情况下,使用其标识Person。 id),这是特别需要与聚合一起使用以将聚合分组时

      projection.add(Projections.groupProperty(id )); 


  4. 使用汇总函数

      projections.add(Projections.min( cars.year)作为( minYear)。); 
    projections.add(Projections.max(cars.year)。as(maxYear));


  5. 设置投影

      criteria.setProjection(突起); 


  6. 使用结果转换器 AliasToBeanResultTransformer 将结果字段映射到POJO

      criteria.setResultTransformer(新的AliasToBeanResultTransformer(Person。类)); 


  7. 获得结果

     列表与LT;人> results =(List< Person>)criteria.list(); 



I've been using spring and hibernate for this past few weeks and I've always been learning something new there.

Right now I've got a problem that I want to solve with Projections in Hibernate.

Suppose there is a model Person and that model has many Car. The following are how the class definitions roughly gonna look like:

public class Person implements java.io.Serializable {
    private Integer id;
    private String name;
    private List<Car> cars;
    private Integer minYear; // Transient
    private Integer maxYear; // Transient
}

public class Car implements java.io.Serializable {
    private Integer id;
    private Integer year;
}

The problem here is I want to get the minYear (maxYear) of each Person to be filled by the earliest year (latest year) of the cars they have.

Later I found a solution to use Projections but I stumbled upon org.hibernate.QueryException: could not resolve property: minYear of: model.Person and here is the code of the db operation:

Criteria criteria = sessionFactory.getCurrentSession().createCriteria("model.Person");
            criteria.add(create(personInstance));
            criteria.createAlias("minYear", "minYear");
            criteria.setProjection(Projections.min("cars.year").as("minYear"));

Is there anyway to store the aggregation value in transient method using Projections because I just want to avoid using plain SQL and HQL as much as possible.

解决方案

Never mind, I've found the solution.

  1. First we need to create alias of the associated object like so

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria("model.Person");
    criteria.createAlias("cars", "cars");
    

  2. Select the needed using Hibernate Projections

    ProjectionList projections = Projections.projectionList();
    projections.add(Projections.property("id").as("id"));
    projections.add(Projections.property("name").as("name"));
    projections.add(Projections.property("cars").as("cars"));
    

  3. Group the result based on the root entity (in this case using its id, Person.id), this is needed especially when used with aggregation to group the aggregation

    projections.add(Projections.groupProperty("id"));
    

  4. Use the aggregate function

    projections.add(Projections.min("cars.year").as("minYear"));
    projections.add(Projections.max("cars.year").as("maxYear"));
    

  5. Set the projection

    criteria.setProjection(projections);
    

  6. Use result transformer AliasToBeanResultTransformer to map the result fields (as specified in step 2 & 4) to the POJO

    criteria.setResultTransformer(new AliasToBeanResultTransformer(Person.class));
    

  7. Get the result

    List<Person> results = (List<Person>) criteria.list();
    

这篇关于将hibernate投影映射到java POJO模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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