1

I want to use OpenEntityManagerInViewFilter to be able to avoid lazyinitialization loading. This is my web.xml configuration:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/mvc-dispatcher-servlet.xml
    </param-value>

</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I am still getting the exeption:

Servlet.service() for servlet [mvc-dispatcher] in context with path [/...] threw exception

Request processing failed; nested exception is 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ...., could not initialize proxy - no Session

Anybody know what am I doing wrong?

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
user2622220
  • 185
  • 1
  • 5
  • 19

1 Answers1

1

Turns out because I had configured my contextConfigLocation like this:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    WEB-INF/mvc-dispatcher-servlet.xml
</param-value>

the dispatcher servlet is not same as applicationContext and it was being loaded twice or something, so the solution was
1.create an applicationContext.xml, 2. then move the entitymanager bean from mvc dispatcher to applicationContext.xml. 3. My web.xml now looks like this:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>oemInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    <init-param>
        <param-name>entityManagerFactoryBeanName</param-name>
        <param-value>entityManagerFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>oemInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
user2622220
  • 185
  • 1
  • 5
  • 19