Announcement Announcement Module
Collapse
No announcement yet.
Both EntityManager and Hibernate Session in Junit tests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Both EntityManager and Hibernate Session in Junit tests

    I've a spring/hibernate based application.
    I'm trying to setup a kind of Junit test that's causing to me many troubles.

    I've a Spring/Hibernate configuration similar to this one

    http://struts.apache.org/2.x/docs/st...-jpa-ajax.html

    When my Junit test starts, first i get a business service.
    in this service i have an entityManager injected through @PersistenceContext

    I call on this service, from the Junit method, a @Transactional one, that populate the database with some entities.
    Code:
    MyService myService = (MyService) applicationContext.getBean("myService");
    myService.aTransactionalMethod();
    Then, from my Junit method i need to create more entities, and I'm using directly the Hibernate Session.

    I obtain the session this way:

    Code:
    			entityManagerFactory = (EntityManagerFactory) webApplicationContext.getBean("entityManagerFactory", EntityManagerFactory.class);
    			entityManager = entityManagerFactory.createEntityManager();
    Session hibernateSession = (Session) entityManager.getDelegate();
    Then, i open a transaction, create some entities, and commit...
    Code:
    Transaction transaction = hibernateSession.beginTransaction();
    // persist some entities.
    transaction.commit();
    On the commit I got the exception
    Code:
    org.hibernate.TransactionException: Transaction not successfully started
    Until I didn't add that transactional service method call at the beginning I had not this problem..

    Ideas?
    thanks
Working...
X