0

I look at this, very popular page and see that it start with

This page describes Hibernate 3.1.x and code shown here does not work in older versions.

So my question very easy: how to implement behavior like this in newest versions of hibernate (4.1+)? Maybe it exist more elegant decision of lazy initialization problem? Any advice and links are welcome.

Divers
  • 9,531
  • 7
  • 45
  • 88

2 Answers2

1

Pretty much the same as Hibernate 3 but reference the Hibernate 4 package:

<filter>
                    <filter-name>hibernateFilter</filter-name>
                    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
                    <init-param>
                            <param-name>singleSession</param-name>
                            <param-value>true</param-value>
                    </init-param>
            </filter>

            <filter-mapping>
                    <filter-name>hibernateFilter</filter-name>
                    <url-pattern>/*</url-pattern>
            </filter-mapping>
Dan
  • 1,030
  • 5
  • 12
1

Take a look at the ThreadLocalSessionContext and ManagedSessionContext classes. It should help you do what you need.

If you look at spring's implementation of the filter, it will most likely be using the ThreadLocalSessionContext class.

Matt
  • 11,523
  • 2
  • 23
  • 33
  • If it possible,can you show code example of this approach? For example what should be in filter and how to open/close session begin/commit transactions. – Divers Aug 20 '12 at 12:32
  • Get the spring source code and use that as a reference. If you still cant figure it out ask for more help. But you have to make an effort first – Matt Aug 20 '12 at 16:14