javahibernatelazy-loadingopen-session-in-view

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.


Solution

  • 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.