user1848637
user1848637

Reputation: 181

Accessing SessionFactory from Jhipster

Jhipster is based on Spring-boot, how can I access Hibernate SessionFactory from a Service in Jhipster. I would like to execute a raw SQL query so I need to create Session.createSQLQuery... I noticed that EntityFactory is used in CacheConfiguration but if I try to inject it as a bean in my Service, I got a NullPointer exception...

Thanks, O.

Upvotes: 1

Views: 1300

Answers (1)

user1848637
user1848637

Reputation: 181

OK I got it. We simply need to add

@PersistenceContext
EntityManager entityManager;

protected Session getCurrentSession()  {
    return entityManager.unwrap(Session.class);
}

in the service class. Then, in the method, juste create the hibernate session :

Session session = getCurrentSession();

My mystake was that I wanted to create Hibernate Session in the constructor of the service.

Upvotes: 1

Related Questions