OpenEntityManagerInViewFilter Problems

前端 未结 3 687
难免孤独
难免孤独 2020-12-16 07:30

I have scoured this site among others for answers in getting OpenEntityManagerInViewFilter to work. I have a standard User object that references a roles object with a many

相关标签:
3条回答
  • 2020-12-16 07:58

    But now!!

    Let me make a guess: the name of your application is: board?

    Correct? then go on and read the remaining answer!

    Yes you have two entity manager, and even two identical application contexts (one app context and one web context) -- So you have every bean twice!

    What happened is: you have only one (relevant) spring configuration file: 'board-servlet.xml' ('persistence-spring-beans.xml' is included in that file so at least it is one big logical file)

    And you create a context from this file twice in the 'web.xml':

    first:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/board-servlet.xml  *****This file references the file with entityManager declared*****
            /WEB-INF/board-security.xml
        </param-value>
    </context-param>
    ...
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    ContextLoaderListener load the application context specified by the files in 'contextConfigLocation' parameter.

    second:

    <servlet>
        <servlet-name>board</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    Dispatcher Servlet create the web application context, which xml file is:

    • named by the init-param 'contextConfigLocation'
    • or, if there is no such paramter, it looks for an file named '/WEB-INF/-servlet.xml'

    (For more details have a look at the Java Doc of FrameworkServlet)

    In your case there is no explicitly named file, so it reads the 'board-servlet.xml' again.

    What you need to do is separate them:

    • remove the <import resource="classpath:persistence-spring-beans.xml"/> from board-servlet.xml
    • change the contextConfigLocation in web.xml that it refers to classpath:persistence-spring-beans.xml and /WEB-INF/board-security.xml direcly
    • (not 100% necessary) separate the 'context:component-scan' so that a component scan in board-servlet.xml scan only for @Controller and the component scan in persistence-spring-beans.xml scan for the others (@Service, @Component, @Repository and @Dao)
    • last step: please tell me that it works now
    0 讨论(0)
  • 2020-12-16 07:58

    Even if I can not answer your question at the moment I can give you some hints.

    First there are the some statements, you wrote in your question:

    1) Move the above declaration to very top of the web.xml

    2) Slap @Transactional around my controller method and/or whole class

    1) The order in which the filters are applied is defined by the order of Filters in the web.xml -- So it is correct that the OpenEntityManagerInViewFilter must run before any other filter that use the Entitys. But in most cases there there are not so much filters that relay on Entities. (my the Security filter, if you have extended it) -- But what I do not know is if it is correct to put the filters after the servlet declaration

    2) If the Layz Init Exception is thrown in a JSP, then this is defently wrong, because it can not influence the JSP


    But what me scared a bit (except from the order in you web.xml that makes it not realy easy to read) is the sitemash filter. My first guess is that sitemash causes the problem. If it is not to complicated, then remove the sitemash filter only for an test. If the problem disapear, then you have identified the causing component.

    0 讨论(0)
  • 2020-12-16 08:08

    You DAOs look normal, only the Annotation '@Dao' is a bit suprising. In normal case one would use '@Repository' -- That is a special form of '@Component' but (and that is not very well documented) it adds some special features.

    One of that special features is an exception translation, and when I remember right there, was one other feature that enhance the Entity Manager annotated bz @PersistenceContext with some extra feature that somehow deals with threads and assigning the right entity manager to the right thread.

    So I would recommend to try to replace @Dao by @Repository.

    0 讨论(0)
提交回复
热议问题