aspose file tools*
The moose likes Object Relational Mapping and the fly likes Questions about LAZY and EAGER loading (JPA 1.0) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Questions about LAZY and EAGER loading (JPA 1.0)" Watch "Questions about LAZY and EAGER loading (JPA 1.0)" New topic
Author

Questions about LAZY and EAGER loading (JPA 1.0)

Will Farquharson
Greenhorn

Joined: Dec 02, 2008
Posts: 20

Hi,

I have a few questions regarding LAZY and EAGER loading on JPAs... if you can answer any of them I'd be grateful to hear your response.

1. Firstly, I can't work out if lazy loading is even working. Even if I specify FetchType LAZY on mapped entities, and use my debugger to examine the entity that came back, I can see the mapped entities all fully populated. I have a sneaky suspicion that examining the entity with the debugger has actually triggered the lazy load, but that sounds like a long-shot. How can I verify it's actually working for sure?

2. I am obsessively deciding which mappings should be EAGER and which should be LAZY. Eventually, I'll need to write a piece of code where it makes more sense to use the opposite (i.e., I need EAGER and LAZY depending on which method is finding the entity). I then need to go back and make the mappings LAZY, and write a NamedQuery to specifically eagerly fetch the things I want using the "FETCH" keyword (only way I can see to get both LAZY and EAGER on the same mapping).

The question is, am I right in being this obsessive about it? Am I really improving performance by selectively eagerly fetching things, considering the EntityManager has a cache anyway? How much worse would the performance be if I just used LAZY everywhere?

3. I'm using the Eclipselink implementation of JPA 1.0. Is the behaviour of this LAZY/FETCH stuff specific to the implementation? If so, what kind of changes are expected between them?

Thank you very much in advance,
Will
Will Farquharson
Greenhorn

Joined: Dec 02, 2008
Posts: 20

I apologise, I realise I should've posted this in the Object Relational Mapping Forum, I only just realised it exists -- is it possible to have the thread moved?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10102
    
165

Moved!

[My Blog] [JavaRanch Journal]
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

Regarding # 1, examining via debugger triggers lazy load.

Regarding # 2, lazy loading has performance benefits. However what you are trying to do is not very elegant & might introduce comlexity. In case your requirement is for a web based application you should look at OpenEntityManagerInViewFilter, this allows for lazy loading of web views.
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html.

The EntityManager cache will only be useful if the data is already loaded. In order to use a combination of eager & lazy loading you will need to compare the options & be sure that you are actually getting benefits. I feel that you can start with keeping everything lazy & eager load data in case you encounter performance issue.

Regarding #3, I have not used Eclipselink so am not sure. However i feel that behaviour should be the same, although there might be some subtle differences.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
1 - In EclipseLink the lazy fields will be null in the debugging until accessed. You should see mirror fields in the object that were weaved for every lazy field.

Ensure you are using the EclipseLink agent to enable dynamic weaving, or running the ant task on your jar to enable static weaving.
If you have not enabled weaving, then everything will be eager.

2 - I would recommend making everything lazy. If a specific use case requires eager, that change it as needed.
For performance in a specific use case you can use join fetching or batch fetching.
EclipseLink also supports fetch groups, and load groups to configure what is loaded/fetch for a specific query.

3 - Lazy is implemented differently in different JPA providers. EclipseLink using byte code weaving either dynamic through and agent, or static. Other providers use other mechanisms, such as byte code generated subclass proxies.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Will Farquharson
Greenhorn

Joined: Dec 02, 2008
Posts: 20

Perfect, thank you for the responses.

I will make everything lazy for now, and when our application becomes larger, I will carry out an optimisation task and change certain mappings to eager, or look into fetch groups as per your advice.

Also, thanks for pointing that out about the debugger -- this is exactly what I'm seeing, and it makes sense now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Questions about LAZY and EAGER loading (JPA 1.0)