Hibernate标准投影非标量值
有没有办法使用Criteria为根实体对象投影多个值?
假设我们有这些类(使用适当的映射):
class Boss {
private String name;
private List<Employee> employees;
// setters and getters and more stuff
}
class Employee {
private String name;
// setters and getters and more stuff
}
然后我试图做到这一点:
public void test() {
Criteria criteria = this.getSession().createCriteria(Boss.class);
criteria.createAlias("employees","employees");
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("name"), "name");
projectionList.add(Projections.property("employees.name"), "subordinatesNames");
criteria.setProjection(projectionList);
criteria.setResultTransformer(new AliasToBeanResultTransformer(BossBean.class));
List<BossBean> results = criteria.list(); // fails here
for (BossBean bossBean : results) {
System.out.println (bossBean);
}
}
这就是Bean的样子(nothign special,只是为了分组值):
public static class BossBean {
private String name;
private List<Strings> subordinatesNames;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Strings> getSubordinatesNames() {
return subordinatesNames;
}
public void setSubordinatesNames(List<Strings> subordinatesNames) {
this.subordinatesNames = subordinatesNames;
}
}
这是一个例外:
2014-06-06 13:37:38 [main] ERROR org.hibernate.property.BasicPropertyAccessor - expected type: java.util.List, actual value: java.lang.String.
我猜是尝试将从Boss
(根对象) - >(A) Employee
(关联) - > name
(值)返回的String
合并到List
。
我想自动神奇地将所有插入到List
。 有没有一种方法可以使用Criteria来实现这一点? 如果没有,我该如何实现它?
提前致谢!
素不相识
胜利者
链接地址: http://www.djcxy.com/p/37111.html