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

最近在springboot中整合jpa的时候碰到一个异常,异常如下

javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1496)

该异常一般出现在执行更新删除操作时出现,jpa要求执行这些操作时需要加上事务管理
当使用@Query调用自定义sql时在执行方法前加上@[email protected]一般就能解决
但是开发过程中由于反复出现调用自定义sql的场景,所以为了调用方便决定利用EntityManager.Query做个封装,然后就出问题了…利用EntityManager执行update语句即使加上了@[email protected]仍然出现Executing an update/delete query的异常,在反复查找后终于找到一个解决方案,如下:

public int sqlUpdate(String sql,String[] arrs){
EntityManager em=emf.createEntityManager();
Query query = em.createNativeQuery(sql);
for(int i = 0; i < arrs.length;i++){
query.setParameter(i+1,arrs[i]);
}
em.getTransaction().begin();
int num = query.executeUpdate();
em.getTransaction().commit();
em.close();
return num;
}

利用EntityManager内的事务控制器进行事务管理
————————————————
版权声明:本文为CSDN博主「言一木」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_37769855/article/details/91437305

原文地址:https://www.cnblogs.com/renjiaqi/p/11613774.html

时间: 2024-10-12 20:45:29

javax.persistence.TransactionRequiredException: Executing an update/delete query的相关文章

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

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query at org.springframework.orm.jpa.EntityManagerFactoryUtil

[Spring Data JPA问题]Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException

JPQL如下: @Modifying(clearAutomatically = true) @Query("UPDATE SyncTestFromTKDO SET stuAns = '' where typeId = ?1") public void updateStuAns(Integer typeid); 项目运行时报错,提示: Executing an update/delete query; nested exception is javax.persistence.Trans

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available..

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call 今天用spring4.2.5版进行数据更新的时候出现了这个问题,粗略的看报错的应该是声明式事务有点问题,可是和以前用的3.0版本的配置一样,问题出在哪里呢,百度也找不到好的答案. 我们知

javax.persistence.NoResultException: No entity found for query

先看这个查询:     public FcLatestSumDeliver findByAccount(String account) {         String jpql = " select a from FcLatestSumDeliver a where a.account = :account";         return entityManager.createQuery(jpql, FcLatestSumDeliver.class).setParameter(&

javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: ResultSet is from UPDATE. No Data.

Java jpa调用存储过程,抛出异常如下: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: ResultSet is from UPDATE. No Data. at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361) at org.hiberna

javax.persistence.PersistenceException: No Persistence provider for EntityManage

还发工具:IDEA14+jdk1.7. 文件结构以及hibernate对应的jar包:如附件中的各图所示. persistence.xml代码: <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/

springData jpa update delete

使用@Modifying + @Query 实现springData 对数据的 update delete public interface ICustomer extends JpaRepository<Customer, Long> {     @Modifying     @Query("delete Customer where isLogicDel = 0 and dispatchDate  = ?1 ")     public int deleteCustome

Caused by java lang NoSuchMethodError javax persistence Ta

1.错误描述 WARN:2015-05-01 16:45:37[main] - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDaoImpl': Injection of resource depen

Caused by: java.lang.ClassNotFoundException: javax.persistence.Entity

1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help | start | stop } 2014-7-12 19:41:17 org.apache.catalina.core.AprLifecycleListener init 信息: Loaded APR based Apache Tomcat Native library 1.1.29 usi