When the project was developed, the database operation was performed in a job with lazy loading, but the following error
Org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role: ..., no session or Sessio N was closed
Lazy loading is configured in the project using Hibernate annotations, not an XML configuration file,
The following is a list of solutions collected from the Internet
1, report the following error
Org.hibernate.LazyInitializationException:could not initialize Proxy-no Session at
Workaround:
In Web. XML, configure the location of the Springsevlet, using Org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter, with the following code:
<Filter> <Filter-name>Spring Openentitymanagerinviewfilter</Filter-name> <Filter-class>Org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</Filter-class> </Filter> <filter-mapping> <Filter-name>Spring Openentitymanagerinviewfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
2, usually in the timed task Quartzjob, or junit test using lazy loading errors, reported the following error
Org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role: ..., no session or Sessio N was closed
Workaround:
Add transaction configuration annotations . The JUnit code is as follows:
@RunWith (Springjunit4classrunner. Class= { "File:src/main/webapp/web-inf/applicationcontext.xml", "file: Src/main/webapp/web-inf/demo-servlet.xml "}) @Transactionalpublicclass Job { ... }
3, there is another way to say is Lazy=false, I again Onetomany | Set fetch= fetchtype in Manytoone annotations. EAGER also does not work, do not know to use the configuration file line not
Hibernate + Spring (quartz) integration lazy (delayed) loading problem