错误javax.persistence.TransactionRequiredException:没有正在进行的事务

时间:2015-12-25 04:33:21

标签: java hibernate jpa

我尝试将实体插入数据库。我有BaseJpaDao类:

@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();
    }

我有子类扩展BaseJpaDao

public class CallJpaDao extends BaseJpaDao<Calls> implements CallDao {

和方法

@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()});
            }
        }
    }

我试过坚持并刷新实体,但我有一个错误:

javax.persistence.TransactionRequiredException: no transaction is in progress

修改

我将依赖关系更改为

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

没有用。

我将@Transactional注释添加到子类 - 没有帮助

1 个答案:

答案 0 :(得分:0)

@Transactional更改为:

@Transactional(Transactional.TxType.REQUIRES_NEW)