Spring boot Unable to evaluate the expression Method threw ‘org.hibernate.LazyInitializationException’ exception. using getter, ManyToMany relation

We Are Going To Discuss About Spring boot Unable to evaluate the expression Method threw ‘org.hibernate.LazyInitializationException’ exception. using getter, ManyToMany relation. So lets Start this Java Article.

Spring boot Unable to evaluate the expression Method threw ‘org.hibernate.LazyInitializationException’ exception. using getter, ManyToMany relation

  1. Spring boot Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception. using getter, ManyToMany relation

    You are trying to use lazy data after closing a transaction, and yes, one of the ways is usage EAGER.
    Another way – usage @Transactional on a method that using this data.

  2. Spring boot Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception. using getter, ManyToMany relation

    You are trying to use lazy data after closing a transaction, and yes, one of the ways is usage EAGER.
    Another way – usage @Transactional on a method that using this data.

Solution 1

You are trying to use lazy data after closing a transaction, and yes, one of the ways is usage EAGER.
Another way – usage @Transactional on a method that using this data.

Original Author Of This Content

Solution 2

I was getting this error:

Method threw ‘org.hibernate.LazyInitializationException’ exception.

This is because currently there is no session present. Hibernate opens a session and closes it, but for “lazy = true” or “fetch = FetchType.LAZY” such fields are populated by proxies. When you try to find the value for such a field, it will attempt to go to the database using the active session to retrieve the data. If no such session can be found, you get this exception.

You can fix it using “lazy=false” or check whether you have used @Transcational properly (try to use this in your service layer than your data access layer), you can also use

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)

OR

@Transactional

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment