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 Loading problems and solutions 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 Loading problems and solutions" Watch "Lazy Loading problems and solutions" New topic
Author

Lazy Loading problems and solutions

Max Galushka
Greenhorn

Joined: Mar 29, 2007
Posts: 8
Hello,

I have a small question related to Lazy loading features:

Let's imagine that we have a separated database layer that performs some operations with database and contains the method with business logic that retrieves some object in transaction and returns it to client.

Let's imagine that client for this business logic database layer is simple Servlet web application that need to display on the page some details of the retrieved object.

If the fields need to be displayed for such object is marked for lazy-loading - we will get an error on the view (Servlet) layer as far as objects returned by business logic are detached.

How can we manage such typical situations on the view (Servlet) layer of the application, what JPA basic/typical techniques exist for working in similar situations?

Thank you in advance.

Thanks.
Maxim Galushka
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Typically picking the point in your application that a business transaction starts and ends and using some sort of interceptor logic to do whatever your JPA implementation requires is the way to go, e.g. Open Session in view in Hibernate.

NB: your " business logic database layer" sounds a little like a poorly designed application. Why would your data access layer also perform business logic?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
There are several solutions to handling LAZY relationship detachment.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Serialization.2C_and_Detaching

Some JPA providers such as EclipseLink, allow LAZY relationships to be accessed after the EntityManager is closed or transaction ends. However if you serialize the objects, you still have the same issue.

Ensuring your data access code instantiates the required relationships by accessing them, or using join fetching may be your best solution.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Max Galushka
Greenhorn

Joined: Mar 29, 2007
Posts: 8
Thanks, that is very helpful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Lazy Loading problems and solutions
 
Similar Threads
Tutorial - J2EE for Beginners
General Design Choice for SCEA or any new Enterprise App
Hibernate or iBatis
Getting started
Data Transfer Object + JPA in EJB 3.1 world