0
votes

I used jboss, and hibernate 4 within jpa (JTA transaction type). In a DAOUtil class, I try to get entity manager, I can get it and do a query. But when I flush entity manager, the error happened.

Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:970)
    at com.vms.dao.hibernate.HibernateDAOSession.flush(HibernateDAOSession.java:288)

I searched on internet, and they say: if we use jpa with jta transaction type, we must inject EntityManager into a ejb, is this correct? If this is correct, I've a concern: how about threadpool for entity manager? I confused alot of thing about that.

Please anyone give my the true way, to use jpa within JTA transaction type.

Added.

I think this issue because I've a getter to get EntityManager outside EJB.

One more question, in JPA with JTA transaction type, I think we could not update by native sql. IN ejb I've below code.

public int executeNativeUpdate(String query, Map<String, Object> params) {
    Query q = em.createNativeQuery(query);
    setParameters(q, params);
    return q.executeUpdate();
}

Error raised when running into code:

Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query

Anyone can explain. I'm beginer in JPA adn JTA. If I use JPA with JTA transaction type and inject the EntityManager into Stateless EJB, EJB container will handle transaction for me, right? I no need do begin/commit a transaction. How about native sql? Can EJB container handle all?

1
Show HibernateDAOSession and DAOUtil codebigGuy

1 Answers

0
votes

If you use JTA transactional type then container manage transactions (application server, or openjpa, or other). In this case you should use EJB, where every method call is wraped in transaction. Create, commit, rollback, manage entity manager - work of container.