i'm writing an application in spring boot 2.0 and in i have a response of an Entity JPA Mapped with nested object dependency.
I load and an entity that is returned to the @RestController and included in response service.
I've noticed that a query executed by hibernate when the object is in controller (out of transactions boundaries) to get nested objects (Lazy defined object reference)
How can it be possibile?
in spring boot Open Session in View is enabled by default.
you have to disable it in application.properties
spring.jpa.open-in-view=false
Because jackson could also try to serialize referenced Lazy defined objects, you need to add jackson datatype for hibernate5 (because you are using spring boot 2)
@Bean
public Module hibernate5Module() {
return new Hibernate5Module();
}
and in your pom.xml
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>
without version because it will be taken by spring-boot parent