将hibernate投影映射到java POJO模型
过去几周我一直在使用spring和hibernate,我一直在那里学习新东西。
现在我有一个问题,我想用Hibernate中的Projections来解决。
假设有一个模型Person
,那个模型有很多Car
。 以下是类定义大致如下所示:
public class Person implements java.io.Serializable { private Integer id; private String name; private List cars; private Integer minYear; // Transient private Integer maxYear; // Transient } public class Car implements java.io.Serializable { private Integer id; private Integer year; }
这里的问题是我想让每个Person
的minYear
( maxYear
)在他们拥有的cars
的最早年份(最近一年)填满。
后来我找到了使用Projections
的解决方案,但我偶然发现了org.hibernate.QueryException: could not resolve property: minYear of: 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。
没关系,我找到了解决方案。
-
首先,我们需要像这样创建关联对象的别名
Criteria criteria = sessionFactory.getCurrentSession().createCriteria("model.Person"); criteria.createAlias("cars", "cars");
-
使用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"));
-
根据根实体对结果进行分组(在本例中使用其id,Person.id),尤其是在与聚合一起使用以对聚合进行分组时
projections.add(Projections.groupProperty("id"));
-
使用聚合函数
projections.add(Projections.min("cars.year").as("minYear")); projections.add(Projections.max("cars.year").as("maxYear"));
-
设置投影
criteria.setProjection(projections);
-
使用结果转换器
AliasToBeanResultTransformer
将结果字段(如步骤2和4中所指定)映射到POJOcriteria.setResultTransformer(new AliasToBeanResultTransformer(Person.class));
-
得到结果
List
results = (List ) criteria.list();