0
0

More than 5 years have passed since last update.

Function of OpenEntityManagerInViewInterceptor Spring MVC interceptor

Posted at

Function of OpenEntityManagerInViewInterceptor

  1. Create a EntityManager instance and bind it to thread when start processing HTTP request
  2. 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0