hibernate what is the right way to load object graph

前端 未结 1 1813
梦谈多话
梦谈多话 2021-01-07 10:17

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.

GrandCa         


        
相关标签:
1条回答
  • 2021-01-07 11:18
    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.

    0 讨论(0)
提交回复
热议问题