lundi 23 mars 2015

Access SessionFactory from Spring Boot Application

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?


Aucun commentaire:

Enregistrer un commentaire