Hibernate与集合的AliasToBean [英] Hibernate AliasToBean with Collection

查看:152
本文介绍了Hibernate与集合的AliasToBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Parent和Child类之间定义了双向一对多关系。我期待执行一个查询,以便我可以返回单个父项及其子项的一个子集。

  public class Parent {
private int id;
私人设置< Child> children = new HashSet< Child>(0);

//其他字段+ getters和setters
}

public class Child {
private父母;
private int age;

//其他字段+ getters和setters
}

我希望获得的输出是:

  public class ParentChildWrapper {
private parent parent;
私人收藏< Child>儿童; //子元素
}

我想过滤父代的id并且孩子的年龄栏。我已经尝试了很多查询组合,以获得所需的输出。



以下内容非常接近,但不会将ParentChildWrapper我定义的bean。我得到的结果是一个对象列表,每个子节点有一个匹配年龄过滤器的实例:

 查询q =会话。 createQuery(
SELECT p,c+
FROM Parent p+
INNER JOIN p.children c+
WHERE p.id =:id+
和c.age =:年龄
);

我也尝试按父母进行分组,以尝试将所有孩子聚合到收集,也无济于事。



很明显,我可以将它分成两个单独的查询,以便选择父项,然后选择我想要的子项。但感觉这应该是一个相当常见的用例。也许我并不是想用hibernate-esque的方式。 解决方案

只是不要使用任何变换器并自行编写循环:

  List< Object []> rows = query.list(); 
父亲=空;
列表< Child> children = new ArrayList< Child>(rows.size());
for(Object [] row:rows){
parent =(Parent)row [0];
children.add((Child)row [1]);
}
ParentChildWrapper result = new ParentChildWrapper(parent,children);

这使得8行代码不是1,而是避免了反射调用,因此可以安全地重构。


I have a bi-directional one-to-many relationship defined between Parent and Child classes. I'm looking to execute a query such that I can return a single parent, and a subset of its children in a bean.

public class Parent {
    private int id;
    private Set<Child> children = new HashSet<Child>(0);

    // other fields + getters and setters
}

public class Child {
    private Parent parent;
    private int age;

    // other fields + getters and setters
}

The output I'm looking to achieve is:

public class ParentChildWrapper {
    private Parent parent;
    private Collection<Child> children; // subset of children
}

I'd like to filter on the parent's id and the child's age column. I've tried a number of query combinations in order to get the desired output.

The following is close, but doesn't group the children in a collection in the ParentChildWrapper bean I've defined. The result I get is a list of objects, with 1 instance per child that matches the age filter:

Query q = session.createQuery(
    "SELECT p, c " +
    "FROM Parent p " +
    "INNER JOIN p.children c " +
    "WHERE p.id = :id " +
    "AND c.age = :age"
);

I've also tried to group by the parent in order to try and aggregate all the children into a collection, also to no avail.

Obviously I could separate this into two separate queries in order to select the parent, and then select the children that I want. But it feels like this should be a fairly common use case. Perhaps I'm not thinking in a hibernate-esque way.

解决方案

Just don't use any transformer and code the loop yourself:

List<Object[]> rows = query.list();
Parent parent = null;
List<Child> children = new ArrayList<Child>(rows.size());
for (Object[] row : rows) {
    parent = (Parent) row[0];
    children.add((Child) row[1]);
}
ParentChildWrapper result = new ParentChildWrapper(parent, children);

This makes 8 lines of trivial code instead of 1, but avoids reflection calls, and thus allows safe refactoring.

这篇关于Hibernate与集合的AliasToBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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