1

Does Hibernate 4 bring any improvements regarding lazy loading?

We are using Hibernate as an JPA provider and want to keep our code clean of hibernate specific stuff. We don't want to use the Hibernate session ojbects. In our application we are using Spring and JSF as well. We figured that there is a OpenSessionInViewFilter and a OpenSessionInViewInterceptor however both only seem to work, when the beans are in request scope. Intercepting of the Spring session scope (@Scope("session")) doesn't seem to work.

Other frameworks allow weaving or enhancement of the beans. Is something similar now possible with Hibernate 4 or where other new features introduces that allow the code to stay free of hibernate specific extentensions? (Some configuration is of course is ok.)

I already checked What's new in Hibernate 4? and saw that they worked on the sessionfactory, however I didn't understand if it helps or not.

Community
  • 1
  • 1
Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • Spring also allows weaving through AOP . Did you check on the @Transactional annotation to define transaction boundaries ? – Aravind A Dec 14 '11 at 10:11
  • What's the question? What would you like to do, and what are the problems that you're having? Your question talks about lazy-loading, then about Spring interceptors, then about weaving. thoses are 3 different subjects. – JB Nizet Dec 14 '11 at 10:15
  • @JBNizet I want to be able to use Hibernate lazy loading without any hassles. I don't want any org.hibernate import in my code and was wondering if lazy loading now works in hibernate 4 without that hibernate specific code has to be implemented. The ViewFilter and ViewInterceptors seem to be too limited. I want to code against the JPA api and nothing else. – Udo Held Dec 14 '11 at 10:21
  • @AravindA we are using `tx:aanotation-driven` and `@Transactional` for writing, however this didn't solve LazyIntializationExceptions when accessing, session objects. – Udo Held Dec 14 '11 at 10:23

3 Answers3

0

Hibernate is a JPA implementation. Just use it using the JPA API only, and you'll be free of Hibernate-specific code. Even when using the Hibernate API directly, lazy-loading is configured using JPA annotations. You don't need any Hibernate-specific annotation to use lazy-loading with Hibernate.

The OpenSessionInView filter/interceptor isn't directly linked to lazy-loading. It just allows letting the session open even after the transaction has ended. Anyway, this filter/interceptor does not force you to have any Hibernate-specific code in your own classes, so I don't really understand where the problem is.

Note that Spring supports JPA as well (and Hibernate is a JPA engine), and has OpenEntityManagerInViewFilter/Interceptor, which play the same role as the OpenSessionInViewFilter/interceptor, but for JPA.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for your answer. As I figured it out the OpenSessionInView filter/interceptor is only working for beans in request scope. It won't be able to handle beans in session scope. Reading the OpenEntityManager documentation it seems to be limited to request scope as well. – Udo Held Dec 14 '11 at 11:17
  • 1
    Yes, the session is closed at the end of the request. – JB Nizet Dec 14 '11 at 11:22
  • Checking the documentation OpenJPA and Eclipselink don't seem to be limited to the request after `weaving` or `enhancement` and I'm wondering if Hibernate improved that behavior with version 4. – Udo Held Dec 14 '11 at 11:27
0

I think you shouldn't have entity beans in session scope. You should have value objects in session scope, and if necessary load the related entity bean during a request.

Are you using Extended PersistenceContexts?

GeertPt
  • 16,398
  • 2
  • 37
  • 61
0

There seem to be no improvements with hibernate 4 regarding LazyLoading for other scopes than the request scope.

Udo Held
  • 12,314
  • 11
  • 67
  • 93