aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA2.0 Lazy Load collection(one-Many) attribute and retriving after em.close() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA2.0 Lazy Load collection(one-Many) attribute and retriving after em.close()" Watch "JPA2.0 Lazy Load collection(one-Many) attribute and retriving after em.close()" New topic
Author

JPA2.0 Lazy Load collection(one-Many) attribute and retriving after em.close()

mohan dilli
Greenhorn

Joined: Dec 19, 2012
Posts: 2
Is there a way to lazy load a collection attribute in an entity and retrivie it after calling entityManager.close().Which i mean retrival out of the entity manager context.

Am using Hibernate flavor of JPA which is of version 3.6.8

Code template might look like this

EntityManager em = (retrive from factory)
SampleEntity se = em.find(SampelEntity.class,45);
em.close();
se.getXXXCollection() - Here i required a lazy loading to happen out of the enity manager context

It would ideally throw a "failed to lazily initialize a collection" exception but thought of getting some suggestions from the forum whether is there any solution.

Please suggest.

Thank you
Mohan
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

No you can't.

You should define your data methods well enough that they do only 1 thing, and is clear on what they do.

Closing the EntityManager suggests that that method's task is done already.

Do NOT do lazy-loading outside your data layer.

-k


[SpringSource Certified Spring Professional - Practice Tests]
mohan dilli
Greenhorn

Joined: Dec 19, 2012
Posts: 2
Thanks for the reply Kathleen Angeles!!!

Does anyone implemented @proxy concept of hiberante to override the proxies generated by hibernate during lazy loading of collections?Can i be able to achieve out of context lazy loading by my own implementation of proxies? Not sure what am asking is right.Please suggest
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2378
    
  28

Even if you did implement something like this, you would run into the risk of dirty reads.
 
Consider Paul's rocket mass heater.
 
subject: JPA2.0 Lazy Load collection(one-Many) attribute and retriving after em.close()