wood burning stoves
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Lazy Loading problems and solutions" Watch "Lazy Loading problems and solutions" New topic

Lazy Loading problems and solutions

Max Galushka

Joined: Mar 29, 2007
Posts: 8

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.

Maxim Galushka
Paul Sturrock

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.


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

Joined: Mar 29, 2007
Posts: 8
Thanks, that is very helpful.
I agree. Here's the link: http://aspose.com/file-tools
subject: Lazy Loading problems and solutions
It's not a secret anymore!