我尝试将实体插入数据库。我有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
注释添加到子类 - 没有帮助
答案 0 :(得分:0)
将@Transactional
更改为:
@Transactional(Transactional.TxType.REQUIRES_NEW)