1

In a Spring Boot (1.5.3) application I used to use the following configuration to be able to inject the Hibernate (5.0.12) session:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate4.SpringSessionContext

When I replace it with:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate5.SpringSessionContext

I get org.hibernate.LazyInitializationException: could not initialize proxy - no Session quite immediately after the first request.

Why does using hibernate5.SpringSessionContext not work with Hibernate 5 ?

Naros
  • 19,928
  • 3
  • 41
  • 71
yglodt
  • 13,807
  • 14
  • 91
  • 127
  • 1
    That property has no effect if you are using plain Hibernate. Also you don't need to (or shouldn't) set it as Spring will do this itself already (basically you are making things more complex this way). Also the error you have has nothing to do with this property, you are trying to access a collection after the session./transaction has closed already. – M. Deinum May 17 '17 at 09:46
  • Ok, thanks for explaining. I have not tried completely removing the property. But note that when I set it to *4. everything works. Just by setting it to *5. I get the "No session" error. I will also try to remove the parameter later today. – yglodt May 17 '17 at 11:36
  • I remember with Spring Boot 1.2 or 1.3 I HAD to set spring.jpa.properties.hibernate.current_session_context_class= org.springframework.orm.hibernate4.SpringSessionContext to be able to autowire the Hibernate session, see my answer here: http://stackoverflow.com/a/33881946/272180 – yglodt May 17 '17 at 11:42
  • Ah... But that isn't something you explained :). When using plain hibernate (which I would recommend against nowadays in favor of JPA). Why do you need the session if you can do the same with an `EntityManager`? – M. Deinum May 17 '17 at 14:14
  • My whole code is based on SessionFactory, and Hibernate queries (HQL and SQL) and I don't want to change that now. When I remove the spring.jpa.properties.hibernate.current_session_context_clas‌​s parameter I get "org.hibernate.HibernateException: No CurrentSessionContext configured!" So the parameter seems still to be required. – yglodt May 17 '17 at 19:02
  • It is if you use the `EntityManagerFactory` to configure the `SessionFactory`. However the other exception you get has to do with transaction boundaries. – M. Deinum May 17 '17 at 21:38

0 Answers0