简体   繁体   中英

Open Session in view for new version of Hibernate

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.

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM