I have this class which is dedicated to persist data in db through persistance layer of hibernate.
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
}
The problem is that it does not persist data.
The stack is:
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)
But i have to note, that it works fine in other classes.
UPDATE:
when i make print of tx.isActive()
it gives me false
.
UPDATE
I tried to more get some info about the error:
I got where the problem is:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: sau.se.domain.model.TLinkEquipement.TEquipementsByIdEquipement2 -> sau.se.domain.model.TEquipements
at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java:376)
in fact, the table TLinkEquipement
has 2 fk to the same table TEquipements
, and me, i persist data of TEquipements
then those of TLinkEquipement
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…