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?
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.
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.