休眠什么是加载对象图的正确方法 [英] hibernate what is the right way to load object graph

查看:111
本文介绍了休眠什么是加载对象图的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3桌GrandCat,Cat和Kitt。他们有一对多的关系,所以我有以下类。所有关联都是延迟加载。

  GrandCat {
int age;
设置< Cat>猫;
}

猫{
int age;
Set< kitt>小猫;
GrandCat grandCat;
}

套件{
字符串颜色;
猫猫;
}

我想建立一个grandCat列表。条件是grandCat.age> 10和Cat.age> 5并且至少有一个颜色为黄色的kitt。当我做grandCat.getCats()时,只有猫满足条件返回。例如,我有以下猫。
关系像这样

  grand 1(age> 10) - > alex(age> 5) - >一个(黄色),b,c | 
|
- > bob(年龄<5) - > d,e(黄色)

盛大2(年龄> 10) - >查理(年龄<5) - > f,g(黄色)
|
- >大卫(年龄> 5) - > h
|
- >爱德华(年龄> 5) - > j(黄色)
|
- > fay(age> 5) - >我(黄色),k(黄色)

其他grandCats ........

我希望GrandCats的回归是grand1和grand2,就像这样

  grand1  - > alex - > a 
grand2 - >爱德华 - > j
|
- > fay - > i,k

,猫和小猫,所以不想加载它们然后过滤。



我知道我可以通过使用


$ b来实现它$ b

 从grandCat中选择g,c,k g内部联合抓取g.cat c内部联合抓取c.kitten k其中g.age> 10和c.age> 5和k.color = '黄色'。 

,然后循环返回值以查看哪只猫属于猫的哪只grandCat和小猫。但这有一些缺点,在grandcat和cat级别上有重复。因为他们返回为

  grand1  - > alex  - > a 
grand2 - > edward- - > j
grand2 - > fay - > i
grand2 - > fay - > k
任何人都有很好的方法来加载这个?我应该使用3 hql吗?首先得到匹配grandCat1和grand2

 从grandCat中选择g g内部联接fetch g.cat c内部联接fetch c.kitten k其中g.age> 10,c.age> 5,k.color ='yellow'。 

然后用上次查询的grandcat查询猫,然后把cat放到grand.setcats( )

 从cat c中选择c内部联接fetch cat.grandCat g内部联接fetch c.kitten k其中g =:grandCat和c.age> 5 and k.color ='yellow'

然后查询小猫,并做同样的事事情?

 从ketk中选择k内部连接获取k.cat c其中c =:cat和k.color ='yellow'

似乎很乏味。

什么是最好的方法来做到这一点?顺便说一下,我希望返回的grandCat和它的猫和小猫仍然具有延迟加载能力,比如说小猫有其他联想,我可以稍后通过延迟加载来使用它们。

 从GrandCat中选择不同的g g 
内部连接读取g.cat c
内部连接读取c.kitten k
其中g.age> 10
和c.age> 5
和k.color ='黄色'

应该很好。返回的grandcat只会在其cats集合中拥有想要的猫,并且每只猫只会在其kitten集合中拥有想要的小猫。

请注意,尽管Hibernate返回实体,这些实体并不反映这些协会的实际情况。我会将它们视为只读对象,而不是试图以任何方式修改关联,因为它可能会产生灾难性的副作用。


suppose I have 3 tables GrandCat, Cat, and Kitt. they have the one to many relation so I have the following classes. all association is lazy loading.

GrandCat{
    int age;
    Set<Cat> cats;
} 

Cat{
   int age;
   Set<kitt> kitten;
   GrandCat grandCat;
}

Kit{
   String color;
   Cat cat;  
}

I want to build a list of grandCat. condition is the grandCat.age > 10 and Cat.age>5 and has at least one kitt which color is yellow. and when I do grandCat.getCats(), only the cats satisfy the condition returns. for example, I have the following cats. relation like this

 grand 1(age>10)--> alex(age>5) ---> a(yellow),b,c             |
                |
                --> bob(age<5)  --->d,e(yellow)

 grand 2(age>10)  --> charlie(age<5) ---> f,g(yellow)
                   |
                   --> david(age>5)--->h
                   |
                   -->edward(age>5)-->j(yellow)
                   |
                   -->fay(age>5) --> i(yellow),k(yellow)

 other grandCats........

I want to have the return GrandCats are grand 1 and grand2 like this

   grand1-->alex-->a
   grand2-->edward-->j
          |
          -->fay-->i,k

they are millons of grandcat, cat, and kitten, so don't want to load them then filter.

I know I can achieve it by using

select g,c,k from grandCat g inner join fetch g.cat c inner join fetch c.kitten k where g.age>10 and c.age>5 and k.color='yellow'. 

and then loop the return value to see which cat belong to which grandCat and kitten to the cat. but this have some drawback, there are duplicates on grandcat and cat level. because they returned as

  grand1-->alex-->a
  grand2-->edward-->j
  grand2-->fay-->i        
  grand2-->fay-->k

so I need to compare them and filter, when there are many records, this take time and consume resources. anyone has a good way to load this? should I use 3 hql? first get matched grandCat1 and grand2

select g from grandCat g inner join fetch g.cat c inner join fetch c.kitten k where g.age>10 and c.age>5 and k.color='yellow'.

then query the cat using the grandcat return from last query and then put the cat to grand.setcats()

select c from Cat c inner join fetch cat.grandCat g inner join fetch c.kitten k where g=:grandCat and c.age>5 and k.color='yellow'

then query the kitten and do the same thing?

select k from Kitt k inner join fetch k.cat c where c=:cat and k.color='yellow'

it seems tedious.

what is the best way to do this? by the way, I hope the returned grandCat and its' cat and kitten still have the lazy loading capability,say if kitten has some other assocation, I can use them in a later time by lazy loading.

解决方案

select distinct g from GrandCat g 
inner join fetch g.cat c 
inner join fetch c.kitten k 
where g.age>10 
and c.age>5 
and k.color='yellow'

should do it fine. The returned grandcat will only have the wanted cats in its cats collection, and each cat will only have the wanted kitten in its kitten collection.

Be aware that although Hibernate returns entities, these entities do not reflect the reality of the associations. I would treat them as read-only objects, and not try to modify the associations in any way, because it could have disastrous side effects.

这篇关于休眠什么是加载对象图的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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