Tuesday, 31 October 2017

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:

org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role: com.yardbud.datamodel.Yard.images, could not initialize proxy - no Session

The exception above is well-known as using hibernate lazy fetching.

As the exception said, "no Session". The error is due to when fetching lazy attributes the session has been closed. typically, you load an entity using Hibernate session and work through it and then close it. The Hibernate doesn't support lazy initialization for a detached entity. Once the session is closed, then the entity cannot make any further call on the database.


So how to solve this issue?
Using static method Hibernate.initialize(entity.getXXX());



References:

The best way to handle the LazyInitializationException

Get org.hibernate.LazyInitializationException in spring boot integration test




Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...