-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to fetch lazy collections along with the child PK
PostPosted: Sun Jan 15, 2017 5:04 pm 
Newbie

Joined: Mon Dec 12, 2016 3:45 pm
Posts: 16
Is it possible to set the child entity's @Id for a lazily fetched collection? I assume the child entity id is fetch when fetching the parent entity.

I would like to return a reference to the child entities in my rest service. For this to be possible, I would need the child.id property to be set for any lazy loaded collections/entities.

I'm using spring data jpa and hibernate.

Is there a way to specify a default entity graph for lazy loaded entities and a different entity graph for when the entity is eager fetched? Any other ways to solve this?


Top
 Profile  
 
 Post subject: Re: hibernate fetch lazy collections with PK
PostPosted: Mon Jan 16, 2017 3:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Lazily fetched child collections are fetched fully when being navigated. More, if you have a parent entity, you need to run a query to fetch all child entity identifiers.

Quote:
I assume the child entity id is fetched when fetching the parent entity.


That's not true. The child side @ManyToOne association is a FK to the parent entity PK. If you only fetch the parent table row, there's no way you also fetch the child entity identifier. The reverse is true. If you fetch the child table row, then you already know the associated parent entity PK.

Quote:
Is there a way to specify a default entity graph for lazy loaded entities and a different entity graph for when the entity is eager fetched? Any other ways to solve this?


Yes, you can. Just follow these steps:

1. You can have the child entity association lazily loaded, which is the best default because EAGER fetching is bad for performance.
2. When you need to fetch the child entities, you either need to navigate the association as long as the Persistence Context is open, or run a JPQL query if the parent entity is detached:

Code:
select c
from Child c
where c.parent = :parent


Top
 Profile  
 
 Post subject: Re: How to fetch lazy collections along with the child PK
PostPosted: Tue Jan 17, 2017 8:23 pm 
Newbie

Joined: Mon Dec 12, 2016 3:45 pm
Posts: 16
that makes sense about the PK.

Thanks for the explanation!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.