Spring Data Jpa javax.persistence. TransactionExceptionRedException:执行更新/删除查询:Sping Boot 2.7.5

lxkprmvk  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(191)

我有下面的错误,而试图做一个update使用Spring Boot 2.7.5 .我已经尝试了不同的方法建议在不同的地方,但仍然得到相同的问题.有一个问题,关于这一点为spring-boot.1.2.5.RELASE在此page .我想知道如果同样的情况发生与Spring Boot 2.7.5 .下面是我的代码:

import org.springframework.transaction.annotation.Transactional;

@Repository
public interface PersonRepo extends CrudRepository<Person, String> {
        
    @Modifying
    @Transactional
    @Query(value = "UPDATE PERSON SET STATUS = 'ACTIVE' WHERE STATUS = :status", nativeQuery = true)
    void updatePersonStatusToActive(@Param("status") String status);
}

字符串
我也试过把@Transactional放在@Repository下面的接口级别,同样的问题。

Stacktrace:

Caused by: 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.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:403)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy113.updatePersonStatusToActive(Unknown Source)
    at com.people.PeopleApplication.processPerson(PeopleApplication.java:304)
    at com.people.PeopleApplication.run(PeopleApplication.java:205)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771)
    ... 5 common frames omitted
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:445)
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1692)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:241)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:156)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:144)
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:160)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
    ... 15 common frames omitted

Person.java

@Entity
@Table(schema = "APP_OWNER", name = "PERSON")
@Data
public class Person {   
    @Id
    @Column(name = "PERSON_ID")
    private String personId;
    
    @Column(name = "FULL_NAME")
    private String fullName;
    
    @Column(name = "ADDRESS")
    private String address;
    
    @Column(name = "STATUS")
    private String status;
    
    @Column(name = "CREATED_DATE")
    private Date createdDate;
}

fae0ux8s

fae0ux8s1#

这有点棘手,因为您没有添加任何生成此错误消息的代码。
您可以尝试使用以下命令进行调试:
1.尝试使用@EnableJpaRepositories@EnableTransactionManagement添加JPA存储库和事务管理,看看是否有任何不同。类似于:

@SpringBootApplication
@EnableJpaRepositories
@EnableTransactionManagement
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

字符串
1.使用Spring接口进行更新有时会出现问题,这可能会导致transaction annotations无法处理。尝试通过Service类执行update操作,而不是直接在main class或其他组件中执行,示例如下:

@Service
public class PersonService {
    
    @Autowired
    private PersonRepo personRepo;
    
    @Transactional
    public void updatePersonStatusToActive(String status) {
        personRepo.updatePersonStatusToActive(status);
    }
}


1.您还可以添加事务日志记录,以便更好地了解根本原因logging.level.org.springframework.transaction=DEBUG

相关问题