title: Hibernate JPA Transaction not active error while running TestCases but works fine when executed in main Class
body: <p>I am trying to insert A Entity Object in Hibernate using jpa persistence along with Guice injection via EntityManager.
  It works fine when i tried to insert an object using my main class that oject entry reflects in my db.
  But when i execute my  Junit testCases it shows me error : "java.lang.IllegalStateException: Transaction not active " while insertion in db Remaining methods works proplerly.</p>
  
  <pre><code>============== AbstractHibernateRepository&lt;T&gt;  Class==================
  @Inject
  private EntityManager em;
  
  
  public AbstractHibernateRepository(Class&lt;T&gt; entityClass) {
      this.entityClass = entityClass;
  }
  
  protected EntityManager getEntityManager() {
      return this.em;
  }
  
  
  
  @Transactional
      public T create(T entity) {
          EntityManager entityManager = getEntityManager();
          if (!entityManager.getTransaction().isActive())
              entityManager.getTransaction().begin();
          try {
              entityManager.persist(entity);
              if (entityManager.getTransaction().isActive()){
                  entityManager.getTransaction().commit();
  
              }
              else
                  entityManager.flush();
          } catch (Exception e) {
              System.out.println("======&gt;&gt;&gt;&gt;&gt;");
              if (entityManager.getTransaction().isActive())
                  entityManager.getTransaction().rollback();
              e.printStackTrace();
          }
          return entity;
      }
  ================================
  ==================  AvailableServerRepository.class ==============
  public class AvailableServerRepository extends
      AbstractHibernateRepository&lt;AvailableServerEntity&gt; implements
      IAvailableServerRepository {
  
  @Override
  public AvailableServerEntity insertAvailableServer(
          AvailableServerEntity availableServer) {
      return create(availableServer);
  
  }
  
  }
  
  ====================================
  
  public class AvailableServerRepositoryTest {
  private static Injector injector = null;
  private static PersistenceInitializer initializer = null;
  
  @BeforeClass
  public static void environmentSetup() {
      injector = Guice.createInjector(new MainModule(), new JpaPersistModule(
              "proplet-indexer"));
      initializer = injector.getInstance(PersistenceInitializer.class);
      initializer.startServer();
  }
  
  @AfterClass
  public static void environmentClean() {
      if(initializer != null)
      initializer.stopServer();
  }
  
  
  /**
   * Test of insertAvailableServer method, of class AvailableServerRepository.
   */
  @Test
  public void testInsertAvailableServer() {
      AvailableServerRepository availableServerRepository = injector
              .getInstance(AvailableServerRepository.class);
      AvailableServerEntity availableServer = createAvailableServer();
                       assertNotNull(availableServerRepository.insertAvailableServer(availableServer));
  }
  
  ====================================
  
  
  
  
  Error while it throws while running testCases is 
  
  java.lang.IllegalStateException: Transaction not active
  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:70)
  at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInterceptor.java:87)
  at com.exzeo.indexer.persistence.repository.impl.AvailableServerRepository.insertAvailableServer(AvailableServerRepository.java:34)
  at com.exzeo.indexer.persistence.repository.impl.AvailableServerRepositoryTest.testInsertAvailableServer(AvailableServerRepositoryTest.java:56)
  </code></pre>