Notice
I wrote this article and was originally published on Qiita on 13 September 2019.
Function of OpenEntityManagerInViewInterceptor
- Create a EntityManager instance and bind it to thread when start processing HTTP request
- Close the EntityManager instance created before when finish processing HTTP request
Why need it?
Assume entity A, its property 'type' set to entity B. Entity A is created by EntityManager, which is set to lazy loading. This EntityManager instance may be closed if a transaction loading entity A is finished. And then property 'type' of enitiy A is accessed on view layer. Since entity B is a proxy instance and being load if necessary. In order to load entity B, EntityManager which created entity A is necessary. At moment on view layer that EntityManager is gone. As result exception occurs.
If OpenEntityManagerInViewInterceptor is enabled, the EntityManager instance is available at the moment on view layer.
How to enable it in Spring Boot?
OpenEntityManagerInViewInterceptor is enabled by default. Or you may enable it explicitly by adding below line in property file 'application.properties'.
spring.jpa.open-in-view=true
Top comments (0)