Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
mohan dilli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you did implement something like this, you would run into the risk of dirty reads.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!