Oxnard
Oxnard

Reputation: 269

Spring cannot get a hibernate session

Using Spring 2.0.5

in the dao class

@Repository
@Transactional
public class IsActiveTestDao {
  private EntityManager em;

 public void populateIsActiveTest(){
    Session session = em.unwrap(Session.class);

when I do this or any 50 other ways like:

SessionFactory f = em.unwrap(SessionFactory.class);

I have also tried

em.getDelegate()

I always get a Null Pointer Exception

My question is Can I obtain a hibernate session from a JPA Entity Manager in Spring while using the @Transactional annotation?

Upvotes: 0

Views: 361

Answers (3)

Sudip Bolakhe
Sudip Bolakhe

Reputation: 523

You are creating a null EntityManager. So, you are getting a null pointer exception You need to fetch the EntityManager object from Application context either by

 @PersistenceContext

or

 @Autowired 

to get the initialized object of EntityManager.

Doing this will work for you.

Upvotes: 0

rifat sahariar
rifat sahariar

Reputation: 53

You can use @PersistenceContext annotation to the EntityManager to specify the persistence unit you want to use.

Upvotes: 0

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

You need to add an @Autowired annotation to the EntityManager

Upvotes: 1

Related Questions