AbhiRam
AbhiRam

Reputation: 2061

How to handle Hibernate SessionFactory in Spring Boot

I am learner and and Does anyone know how to get a handle the Hibernate Session Factory that is created by Spring Boot? i seen number of solutions and i implement below code but i am getting exception when hit service

org.hibernate.HibernateException: No CurrentSessionContext configured!

application.properties

spring.datasource.url= jdbc:mysql://localhost:3306/sakila
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

configuration

@SpringBootApplication
public class SpringBootCurd1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCurd1Application.class, args);
    }

    @Bean
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
        return hemf.getSessionFactory();
    }

}

dao

@Repository
public class EmployeeDao {

    @Autowired
    SessionFactory sessionFactory;

    /**
     * getListOfStates
     * 
     * @return
     */
    public List<?> getListOfStates(int userId) {

        String hql = "FROM states s WHERE " + "s.userid = :userId";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setParameter("userId", userId);
        session.flush();
        session.clear();
        return query.list();

    }

}

Upvotes: 0

Views: 6613

Answers (1)

M. Deinum
M. Deinum

Reputation: 124441

Don't use plain hibernate but use JPA as the API, unless you really need specific Hibernate specific features (which looking at your code you don't need).

So rewrite your code to use an EntityManager instead:

Repository
public class EmployeeDao {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * getListOfStates
     * 
     * @return
     */
    public List<State> getListOfStates(int userId) {
        return em.createQuery("SELECT s FROM states s WHERE s.userid = :userId", State.class)
            .setParameter("userId", userId)
            .getResultList();

    }
}

Now if you really need access to the Session use EntityManager.unwrap(Session.class) to obtain it.

@Repository
public class EmployeeDao {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * getListOfStates
     * 
     * @return
     */
    public List<?> getListOfStates(int userId) {

        String hql = "FROM states s WHERE " + "s.userid = :userId";
        Session session = entityManager.unwrap(Session.class);
        Query query = session.createQuery(hql);
        query.setParameter("userId", userId);
        session.flush();
        session.clear();
        return query.list();

    }
}

However as stated you should really prefer JPA over the plain API of Hibernate.

NOTE: You can now also remove the @Bean method that produces a SessionFactory.

Upvotes: 6

Related Questions