Hibernate + Spring (quartz) integration lazy (delayed) loading problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.