| 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: 550
|
|
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.
|
 |
 |
|
|
subject: Lazy Loading problems and solutions
|
|
|