0

I tried insert entities to DB. And I have BaseJpaDao class:

@Transactional(Transactional.TxType.REQUIRED)
public abstract class BaseJpaDao<E> implements BaseDao<E> {

    protected Class<?> entityClass;
    private EntityManager em;

    public BaseJpaDao(Class<?> entityClass) {
        this.entityClass = entityClass;
        em = Persistence.createEntityManagerFactory("COLIBRI").createEntityManager();
    }

    @Override
    public E persist(E e) {
        em.persist(e);
        return e;
    }
...
@Override
    public void flush() {
        em.flush();
    }

And I have child class extends BaseJpaDao

public class CallJpaDao extends BaseJpaDao<Calls> implements CallDao {

and method

@Override
    public void insertCalls(List<Calls> callsList) {
        for (Calls call : callsList) {
            try {
                persist(call);
                flush();
            } catch (Exception e) {
                logger.log(Level.FINEST, "Exception in  task time={0}. Exception message = {1}.", new Object[]{call.getDate(), e.getMessage()});
            }
        }
    }

I tried persist and flush entity but I have an error:

javax.persistence.TransactionRequiredException: no transaction is in progress

EDIT

I change dependency to

<dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>javax.ejb-api</artifactId>
            <version>3.2</version>
        </dependency>

It not helped.

And I add @Transactional annotation to child class - not helped

user5620472
  • 2,722
  • 8
  • 44
  • 97
  • See http://stackoverflow.com/questions/1801828/hibernate-jpa-and-spring-javax-persistence-transactionrequiredexception-no-tran and http://stackoverflow.com/questions/23314953/hibernate-spring-javax-persistence-transactionrequiredexception-no-transacti Ensure your application is annotation-driven. Google how to do that with your version of Spring. – dseibert Dec 25 '15 at 04:37
  • I don't use SPRING I use JavaEE EJB – user5620472 Dec 25 '15 at 04:41
  • Interesting. Thanks for that information. Check the link below out. It may be due to a limitation of your EJB version. It seems like your application is ignoring the @Transactional annotation and either another annotation is needed or you need to rethink how you're working with the EntityManager. http://stackoverflow.com/questions/27274216/using-transactional-with-ejbs – dseibert Dec 25 '15 at 04:48
  • Have you tried putting @Transactional on the method insertCalls(), as opposed to the abstract class? – dseibert Dec 25 '15 at 04:52
  • It isn't recommended (with Spring anyway) to annotate superclasses with @Transactional. See this post http://stackoverflow.com/questions/23736231/does-spring-transaction-work-on-a-concrete-method-of-abstract-class – dseibert Dec 25 '15 at 04:57
  • Are the classes included in package scan? How do you create the CallJpaDao? Is it autowired? It should not be new CallJpaDao – StanislavL Dec 25 '15 at 05:35
  • CallJpaDao is singleton(I use pattern singltone because in javaEE not autowired) – user5620472 Dec 25 '15 at 05:38
  • `@Transactional` annotation only works, if you inject the object using `@Inject`. Can you add the vode, where you create the instance of CallJpaDao and call insertCalls method? – OndroMih Dec 25 '15 at 09:07

1 Answers1

0

Change @Transactional to :

@Transactional(Transactional.TxType.REQUIRES_NEW)
Tom Sebastian
  • 3,373
  • 5
  • 29
  • 54
  • It seems like both should supply a new transactional scope within the calling method. http://docs.oracle.com/javaee/7/api/javax/transaction/Transactional.TxType.html – dseibert Dec 25 '15 at 04:52
  • 1
    try annotating child class with @Transactional – Tom Sebastian Dec 25 '15 at 04:53
  • AFASIK annotations would work only when there is a container environment.It seems your are running your program in a SE environment. So you need to start transaction explicitly(EntityManager.getTransaction().begin()). OR you can run using JEE embedded container which would take of transactions for you. – Mandroid Dec 25 '15 at 15:51