12

I am trying to get access to the Hibernate session factory but am getting the following error at the line mentioned.

No CurrentSessionContext configured!

code

@Service
@Transactional
public class GenericSearchImpl implements GenericSearch {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Override
    @SuppressWarnings("unchecked")
    public <T> List<T> search(final Class<T> type, final String[] criteriaList, final int page, final int perPage) {
        Session session = getSession();
        ...
    }

    public Session getSession() {
        final HibernateEntityManagerFactory emFactory = (HibernateEntityManagerFactory) entityManagerFactory;
        final SessionFactory sessionFactory = emFactory.getSessionFactory(); 
        return sessionFactory.getCurrentSession(); //ERROR No CurrentSessionContext configured!

          //This worked but I understand it to be BAD as spring should be managing open sessions.
          //        try {
          //            return sessionFactory.getCurrentSession();
          //        } catch (Exception e) {
          //            return sessionFactory.openSession();
          //        }
    }

    ...

}

Any idea why?

jax
  • 37,735
  • 57
  • 182
  • 278
  • JPA doesn't use the `CurrentSessionContext` class. Configuring it should solve your problem. Simply add the configuration for the current session context and let it point to the `org.springframework.orm.hibernate4.SpringSessionContext` class. However why are you trying this? WHy not use plain JPA? – M. Deinum Mar 24 '15 at 06:53
  • @M.Deinum I am doing this to support some legacy code that is using Hibernate Criteria. It will eventually be converted over to JPA. The rest of the application is using spring-data-jpa, I only need the Hibernate SessionFactory temporarily until it can be rewritten. Will a hybrid approach like this work? Can you give an example of configuring CurrentSessionContext as a spring bean in java config? – jax Mar 24 '15 at 08:37
  • Is [this](http://stackoverflow.com/questions/26667910/no-currentsessioncontext-configured?answertab=active#tab-top) what you mean? – jax Mar 24 '15 at 08:39
  • Yes it is (and as that is basically the same question, marked this one as duplicate). – M. Deinum Mar 24 '15 at 08:50
  • http://stackoverflow.com/questions/18759978/hibernate-entitymanager-with-getsessionfactorygetcurrentsession. Please refer this. Shows you how exactly this can be done. – ArunM Mar 24 '15 at 08:52
  • @M.Deinum I followed that tutorial but still get `No CurrentSessionContext configured!` – jax Mar 24 '15 at 21:32
  • Just did an update on that, it is missing a crucial configuration property. – M. Deinum Mar 25 '15 at 07:04

2 Answers2

13

In property file,

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

in configuration class

@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
    return new HibernateJpaSessionFactoryBean();
}

Then you can autowire

@Autowired
private SessionFactory sessionFactory;

We do this as Spring boot doesn't auto configure hibernate sessinoFactory.

Update: As of Spring 4.3.12 and Hibernate 5.2, above Hibernate API solution is depreciated in favor of generic JPA API solution EntityManagerFactory.

Session session = entityManager.unwrap(Session.class);

Here is some detailed example doc with examples on EntityManagerFactory.

Anand
  • 9,672
  • 4
  • 55
  • 75
6

You can access SessionFactory with entityManagerFactory unwrap method instead of HibernateJpaSessionFactoryBean

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

HibernateJpaSessionFactoryBean is deprecated in Spring Boot 1.5.8

icaglar
  • 101
  • 2
  • 2