title: Transaction not active - Hibernate - JPA
body: <p>I have this class which is dedicated to persist data in db through persistance layer of hibernate.</p>
  
  <pre><code>public class TLinkEquipementDAOImpl implements TLinkEquipementDAO {
  
      private static final Log log = LogFactory
              .getLog(TLinkEquipementDAOImpl.class);
  
      @PersistenceContext
      private EntityManagerFactory emf = PersistenceManager.getInstance()
              .getEntityManagerFactory();
      private EntityManager entityManager = emf.createEntityManager();
  
      private EntityTransaction tx = entityManager.getTransaction();
  
      public void persist(TLinkEquipement transientInstance) {
          log.debug("persisting TLinkEquipement instance");
          try {
              tx.begin();
              entityManager.persist(transientInstance);
              tx.commit();
              log.debug("persist successful");
          } catch (RuntimeException re) {
              tx.rollback();
              log.error("persist failed", re);
              throw re;
          }
      }
  //Staff
  }
  </code></pre>
  
  <p>The problem is that it does not persist data.</p>
  
  <p>The stack is:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: Transaction not active
      at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:82)
      at sau.se.domain.dao.Impl.TLinkEquipementDAOImpl.persist(TLinkEquipementDAOImpl.java:47)
      at sau.se.domain.service.Impl.TLinkEquipementServiceImpl.persist(TLinkEquipementServiceImpl.java:29)
      at sau.se.extractor.InfoExtract.getAllSPData(InfoExtract.java:346)
      at sau.se.extractor.InfoExtract.main(InfoExtract.java:436)
  </code></pre>
  
  <p>But i have to note, that it works fine in other classes.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>when i make print of <code>tx.isActive()</code> it gives me <code>false</code>.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried to more get some info about the error:</p>
  
  <p>I got where the problem is:</p>
  
  <pre><code>Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: sau.se.domain.model.TLinkEquipement.TEquipementsByIdEquipement2 -&gt; sau.se.domain.model.TEquipements
      at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java:376)
  </code></pre>
  
  <p>in fact, the table <code>TLinkEquipement</code> has 2 fk to the same table <code>TEquipements</code>, and me, i persist data of <code>TEquipements</code> then those of <code>TLinkEquipement</code></p>