This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Lazy relationships become loaded after EntityManager closes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Lazy relationships become loaded after EntityManager closes" Watch "Lazy relationships become loaded after EntityManager closes" New topic
Author

Lazy relationships become loaded after EntityManager closes

Braiam Yesid
Greenhorn

Joined: Feb 26, 2010
Posts: 2
Hello guys

Using JPA 2.0, I do not want that lazy loaded relationships become loaded after closing EntityManager.

This is what I'm using
Netbeans 6.8
PostgreSQL 8.4
EclipseLink 2.0

I mean, If an Entity object has a (lazily loaded) collection of other Entities, I expect that after closing the EntityManager, those relationships are not loaded. I want null fields or a NullPointerException. But what I'm getting after closing the EntityManager is the full node tree.

Class A


Class B


Main Class


Output



As you can see, B may be too big to keep in memory. For example, I want to list all the 500 instances of A. And each A has 100 B, so I have a total of 5000 B objects. I want ONLY the list of A, without their B's loaded. But I'm getting all the 500 A's and all the 5000 B's.

Am I missing some point?

Thank you guys
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
It is odd that you would want a null-pointer exception when accessing your relationship instead of its value.

If you do not wish to have the value instantiated, then do not access it.

Otherwise, you could clone or serialize the object and leave the lazy relationship as null.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy relationships become loaded after EntityManager closes
 
Similar Threads
Hibernate Criteria can't create joins for Embedded objects
EJB3 org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role
@ManyToMany and Select query
Problem while One to One Relationship
Hibernate One-To-Many bidirectional