Announcement Announcement Module
Collapse
No announcement yet.
Spring, JSF, Hibernate & Lazy Collections Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, JSF, Hibernate & Lazy Collections

    I am working on a web application using JSF, SPring and Hibernate.

    On one of my JSP pages I need to make a reference using a JSF value binding to a property that has been defined to be a lazy collection in Hibernate. Obviously at the beginning I received the typical "not in session" exception from Hibernate, as the session was not injected using the ThreadLocal pattern.

    So one solution I thought was to take advantage of Spring's declarative transactionality using Hibernate transaction manager, i.e. I defined my managed bean in Spring's context as being a TransactionalProxyFactoryBean object, indicating the corresponding transaction attributes.

    But when I run the application, and use JSF value binding to access any property of my original managed bean, it turns out that it is the proxy that wraps my managed bean who gets called, and a PropertyNotFound exception is thrown.

    So I realized that I could do the following:

    <bean id="userBean"
    class="mypackage.UserBean">
    </bean>

    <bean id="userBeanProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager" />
    </property>
    <property name="target">
    <ref local="userBean" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="myaction">
    PROPAGATION_REQUIRED,readOnly
    </prop>
    </props>
    </property>
    </bean>

    And then, within my JSP page, if I need to refer to one of my managed bean property, then I use this value binding:

    value="#{userBean.myproperty}"

    But if I want to go to another page that will make use of the lazy collection for my managed bean, then I use this action binding:

    action="#{userBeanProxy.myaction}"

    However, it seems that JSF does not know how to work with the proxy, I don't understand why, and does not call that method whenever the button is pressed.

    Any ideas on how to solve my problem with lazy collections?

  • #2
    Obviously at the beginning I received the typical "not in session" exception from Hibernate, as the session was not injected using the ThreadLocal pattern.

    So one solution I thought was to take advantage of Spring's declarative transactionality using Hibernate transaction manager, i.e. I defined my managed bean in Spring's context as being a TransactionalProxyFactoryBean object, indicating the corresponding transaction attributes.
    Transaction management will not solve your in view, lazy initialization problems. For that you need a filter - look at OpenSessionInViewFilter and OpenSessionInViewInterceptor from orm.hiberante.support packages.
    This way you'll basically have the session opened during render time.

    However if you want the session to be spawn agains several request (you mention something about 'other pages') then you have to supply your own solution (ie either eager fetch the collection an save it somewhere or reattach the object to another session again).

    Comment


    • #3
      Does this issue has something to do with OpenSessionInViewFilter?

      Comment


      • #4
        Yes, I thought it could be explained from a different point of view...

        Comment

        Working...
        X