3

I'm using JPA2.1 and hibernate 4.3.8 and i have configured the presistence.xml to allow lazy loading

i have added

<property name="hibernate.enable_lazy_load_no_trans" value="true" />

into properties section

but i'm still getting LazyInitializtionException, what is the problem ?

2
  • Is the property in the same format as this answer? Commented Jun 26, 2015 at 14:50
  • yes, the problem was in hibernate version Commented Jun 27, 2015 at 15:41

3 Answers 3

11

The hibernate.enable_lazy_load_no_trans is an anti-pattern and you should never use it because a database connection is needed for every lazy association that is fetched outside of the initial Persistence Context, and that's going to put pressure on the underlying transaction log and the JDBC connection pool.

More, the hibernate.enable_lazy_load_no_trans is prone to N+1 query issues.

Sometimes, you don't even need entities, and a DTO projection is even better.

Sign up to request clarification or add additional context in comments.

7 Comments

"never use it" -> "avoid if possible". This parameter was the best option for our use case (max 20 users, big entity graph that gets 100% displayed in a GUI (in tabs, tooltips & dialogs)).
Just because you don't have a typical enterprise concurrency or data requirements, it does not mean that this setting will buy you a lot. As I explained in the links attached to this answer, the LazyInitializationException is a code smell related to the data access layer design. Most often, entities are fetched for read-only views even if DTOs would suffice. Remember that entities are only needed if you plan on modifying them. Otherwise, you're just burning DB resources to fetch more columns than necessary.
In the real world most legacy projects have code smells like this and while I agree with all your points, you have to work with what you're given. This parameter was a real time-saver because it allowed me to simply switch the model to lazy loading to improve performance. 1 mwd vs 15 mwd to rework the whole thing. Read-only operations (to generate reports didn't use DTOs so the whole graph was eager loaded every time the reports needed an attribute of the main entity).
Or if the previous team decided that it would be a good idea to eager load everything with Hibernate. Which worked fine in production for a while because the same data was edited over (= not a lot of new records). The problem only became apparent after a few years of slowly adding new records. I definitely wouldn't use that parameter in a new project but it did great for that old one.
Your book discourages Temporary Session Lazy Loading but doesn't tell us to never use it. This is the only thing that bothers me here as it was the best (= cost-effective) solution to our problem.
|
0

Try enter the "true" like this:

<property name="hibernate.enable_lazy_load_no_trans">true</property>

It work for me.

Comments

0

org.hibernate.LazyInitializationException:

Use this in application.properties if u are working with getReferenceById(id) method

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.