hibernate hql如何从父项中获取匹配的子项? [英] hibernate hql how to get matched child from parent?

查看:64
本文介绍了hibernate hql如何从父项中获取匹配的子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两张桌子,猫和小猫(一对多),小猫桌上有5条记录,猫桌上有1条记录。小猫有颜色,白色2和黄色3。我写了下面的HQL返回Cat。

 从cat c内部连接c中选择c c.kitten k其中k.color = 'yellow'

这会让我回到猫身上。但是当我做了
$ b $ pre $ cat.getKitten()。size()

它返回5,而不是3.我想要做的只是获得匹配的孩子而不是所有的孩子,那么做到这一点的正确方法是什么? / p>

似乎是这样做的,谢谢zzz和每个人的努力

  Session session = sessionFactory.getCurrentSession(); 
列表< Object> funds = session.createQuery(select k.cat,k from kitten k where k.color ='yellow')。list();
Object [] os =(Object [])funds.get(0);
Cat c =(Cat)os [0];
Kitten fc =(Kitten)os [1];
List list = new ArrayList< Kitten>();
list.add(fc);
c.setKittens(list);

这会返回正确的结果。

解决方案

顺便说一句,这是设计。你不能通过使用限制来过滤集合。



如果你仔细观察,hibernate将会访问数据库两次:一次使用查询,一次获取



您需要先获取您的猫对象,然后获取过滤后的小猫,默认情况下会延迟加载。



类似于

  var kittens = cat.kittens.where(x-> x.color =yellow) 
var count = kittens.Count();

如果java可以这样做

 收集和LT;小猫>小猫= cat.getKittens(); 
Collection< Kitten> filteredKittens = filterCollection(kittens,session);

,并且您的filterCollection方法将为:

  private static< T>收集和LT; T> filterCollection(Collection< T> collection,Session s){
Query filterQuery = s.createFilter(collection,where this.color ='yellow');
返回filterQuery.list();
}

这里是一篇blogpost abt it: http://www.flexpasta.com/index.php/2009/05/20 / filtering-hibernate-child-collections / 基于以下评论我们决定使用



 从grandCat中选择g,k g内部联接抓取g.cat c内部联接抓取c.kitten k其中k.color ='yellow'

并选择所需的内容,然后将其转换为适当的内容。

two tables, Cat and kitten(one to many), there are 5 records in the kitten table, and 1 in the cat table. kitten have color, 2 in white and 3 in yellow. I wrote the following HQL which return the Cat.

select c from Cat c inner join c.kitten k where k.color='yellow'

this return me the cat. but when I did

cat.getKitten().size() 

it returns 5, instead of 3. what I want to do is only get the matched child instead of all of them, what's the right way to do this?

seems like this do the trick, thanks zzz and everyone's effort

Session session = sessionFactory.getCurrentSession();
        List<Object> funds = session.createQuery("select k.cat,k from kitten k where k.color='yellow'").list();
        Object[] os = (Object[])funds.get(0);
        Cat c = (Cat)os[0];
        Kitten fc = (Kitten)os[1];
        List list = new ArrayList<Kitten>();
        list.add(fc);
        c.setKittens(list);

this return the right thing.

解决方案

btw that is by design. You can't filter a collection by using a restriction.

If you look closer, hibernate is going to the DB twice: once with your query, and once to get the collection.

you need to get your cat object first and then get the filtered kittens which will be lazyloaded by default.

something like

var kittens= cat.kittens.where(x->x.color="yellow")
var count=kittens.Count();

if java you could do something like this

Collection<Kitten> kittens= cat.getKittens();
Collection<Kitten> filteredKittens = filterCollection(kittens, session);

and your filterCollection method will be:

private static <T> Collection<T> filterCollection(Collection<T> collection, Session s) {
        Query filterQuery = s.createFilter(collection, "where this.color='yellow'");
        return filterQuery.list();
    }

here is a blogpost abt it: http://www.flexpasta.com/index.php/2009/05/20/filtering-hibernate-child-collections/

based on the comments below we decided to use

select g, k from grandCat g inner join fetch g.cat c inner join fetch c.kitten k where k.color ='yellow' 

and select the required content and then cast it appropriatelty.. read the comments for clarity

这篇关于hibernate hql如何从父项中获取匹配的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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