将hibernate投影映射到java POJO模型 [英] Map hibernate projections result to java POJO model
问题描述
过去几周我一直在使用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
-
首先,我们需要创建关联对象的别名,比如
Criteria criteria = sessionFactory.getCurrentSession()。createCriteria(model.Person);
criteria.createAlias(cars,cars);
-
选择所需的使用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));
-
对基于实体的结果进行分组(在这种情况下,使用其标识Person。 id),这是特别需要与聚合一起使用以将聚合分组时
projection.add(Projections.groupProperty(id ));
-
使用汇总函数
projections.add(Projections.min( cars.year)作为( minYear)。);
projections.add(Projections.max(cars.year)。as(maxYear));
-
设置投影
criteria.setProjection(突起);
-
使用结果转换器
AliasToBeanResultTransformer
将结果字段映射到POJO
criteria.setResultTransformer(新的AliasToBeanResultTransformer(Person。类));
-
获得结果
列表与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.
First we need to create alias of the associated object like so
Criteria criteria = sessionFactory.getCurrentSession().createCriteria("model.Person"); criteria.createAlias("cars", "cars");
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"));
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"));
Use the aggregate function
projections.add(Projections.min("cars.year").as("minYear")); projections.add(Projections.max("cars.year").as("maxYear"));
Set the projection
criteria.setProjection(projections);
Use result transformer
AliasToBeanResultTransformer
to map the result fields (as specified in step 2 & 4) to the POJOcriteria.setResultTransformer(new AliasToBeanResultTransformer(Person.class));
Get the result
List<Person> results = (List<Person>) criteria.list();
这篇关于将hibernate投影映射到java POJO模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!