LazyInitializationException: What it is and the best way to fix it
Вставка
- Опубліковано 28 тра 2024
- The LazyInitializationException is one of the most common exceptions when working with Hibernate. There are a few easy ways to fix it. But unfortunately, you can also find lots of bad advice online. The proclaimed fixes often replace the exception with a hidden problem that will cause trouble in production. Some of them introduce performance issues, and others might create inconsistent results.
Hibernate throws the LazyInitializationException when it needs to initialize a lazily fetched association to another entity without an active session context. That’s usually the case if you try to use an uninitialized association in your client application or web layer.
In this video, I will explain to you what the LazyInitializationException is, which advice you should ignore, and how to fix the exception instead.
But before we proceed, if you are new here and want to learn how to build incredible efficient persistence layers with JPA and Hibernate and all types of other Java persistence related stuff, start now by subscribing and clicking the bell, so you don't miss anything.
Links mentioned:
6 Hibernate Mappings You Should Avoid for High-Performance Applications
thorben-janssen.com/6-hiberna...
Best Practices for Many-to-Many Associations with Hibernate and JPA
thorben-janssen.com/best-prac...
JPA Entity Graphs: How to Define and Use a @NamedEntityGraph
thorben-janssen.com/jpa-21-en...
Entities or DTOs - When should you use which projection?
thorben-janssen.com/entities-...
Like my channel? Subscribe!
➜ bit.ly/2cUsid8
Join the free Member Library:
goo.gl/dtyIIC
Read the accompanying post: thorben-janssen.com/lazyiniti...
Want to connect with me?
Blog: thorben-janssen.com/
Twitter: / thjanssen123
Facebook: / thoughtsonjava
#LazyInitializationException #JPA #Hibernate - Наука та технологія
8k views only. This video contains at least two interview questions.
Thank you for everything Thorben.
Thanks! Need deep explanation JPA Graph Entity with example!
So when you say use Graph or join fetch clause you mean not using the regular find methods of the entity manager and executing the query within the same session ??
Also, One day I fixed the exception using the @Transactional annotation. Does that mean, I opened the session again to load the lazy field ?
Please let me know what your thoughts are. thanks
like always very nice video.
thanks for helping me because I face LazyInitializationException many times but didn't able to understand which solution is a batter choice.
I am glad this helped.
while fetching data from the database sometimes we get recursive data with a stack overflow error, so which is the best way to solve it @jsonIgnore or @jsonBackReferecnce?
everyone encounters this once while working with hibernate so what's your answer for this sir?
Is JPA 2.1 Supports JRE 7
while fetching data from the database sometimes we get recursive data with a stack overflow error, so which is the best way to solve it @jsonIgnore or @jsonBackReferecnce?
everyone encounters this once while working with hibernate so what's your answer for this sir?
Never got
If we declare entire method with @Trasactional then will not get this exception. This way is correct or not?
You asked what I was thinking man. The other day even I fixed the issue by marking the method as @Transactional of spring framework.
I think that opened the session again for the code we wrote in the method.
But my technical manager accepted the solution. I hope he replies to this.
I don't use @Transactional, I think you should use FTP instead
Should we use @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) on the class causing this error?
This seems to fix the issue.