我正在尝试构建一个较小的SQL,以避免默认情况下为hibernate Criteria构建的“select * from A”.
如果我使用简单的字段(无关系),通过“变形金刚”,我可以设法拥有这个SQL:
select description, weight from Dog;
嗨,我有这个实体:
@Entity
public class Dog
{
Long id;
String description;
Double weight;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "person_id", nullable = false)
Person owner;
}
@Entity
public class Person
{
Long id;
String name;
Double height;
Date birthDate;
}
我的目标是拥有:
select description, weight, owner.name from Dog
我用Criteria(和子标准)尝试了这个:
Criteria dogCriteria = sess.createCriteria(Dog.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("description"), description);
proList.add(Projections.property("weight"), weigth);
dogCriteria.setProjection(proList);
Criteria personCriteria = dogCriteria.createCriteria("owner");
ProjectionList ownerProList = Projections.projectionList();
ownerProList.add(Projections.property("name"), description);
dogCriteria.setProjection(ownerProList); //After this line, debugger shows that the
//projection on dogCriteria gets overriden
//and the query fails, because "name" is
//not a field of Dog entity.
我应该如何使用Projections,以获得更小的SQL,更少的列?
提前致谢.
解决方法:
首先,
select description, weight, owner.name from Dog
是无效的SQL.它必须是这样的
select description, weight, Person.name
from Dog join Person on Dog.person_id = Person.id
代替.其次,为什么?虽然可以做你想做的事情(见下文),但是通过Criteria API这样做非常冗长,你无需为此付出任何代价.除非所述列是巨大的blob或者您选择了数十万条记录,否则几列的数据传输节省可以忽略不计.在任何一种情况下,都有更好的方法来处理这个问题.
任何人,为了做你想要的标准,你需要通过别名加入链接表(Person)并使用所述别名指定主要标准的投影:
Criteria criteria = session.createCriteria(Dog.class, "dog")
.createAlias("owner", "own")
.setProjection( Projections.projectionList()
.add(Projections.property("dog.description"))
.add(Projections.property("dog.weight"))
.add(Projections.property("own.name"))
);
在Criteria Projections documentation中有上述描述和示例.请记住,执行时,上述条件将返回对象数组列表.您需要指定ResultTransformer才能将结果转换为实际对象.