• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions about LAZY and EAGER loading (JPA 1.0)

 
Greenhorn
Posts: 20
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved!
 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Will Farquharson
Greenhorn
Posts: 20
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic