gpt4 book ai didi

spring-boot - Spring Boot : javax. persistence.TransactionRequiredException:执行更新/删除查询

转载 作者:行者123 更新时间:2023-12-01 09:56:45 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 并像这样配置我的应用程序:

@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@ComponentScan
@EntityScan("ch.xy.model")
public class Application {

@Autowired
private ImportDAO importDao;

}

ImportDAO 看起来像这样:
@Repository
public class ImportDAO {

@PersistenceContext
private EntityManager em;

@Transactional
void removeTempoAccounts() {
Query q = em.createQuery("DELETE FROM TempoAccount t WHERE t.manual = false");
q.executeUpdate();
}
}

但是当 removeTempoAcconts 被执行时,我得到:
Exception in thread "main" javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:360)
at com.sun.proxy.$Proxy49.executeUpdate(Unknown Source)
at ch.post.pf.jira.tempocats.pspimport.ImportDAO.removeTempoAccounts(ImportDAO.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at ch.post.pf.jira.tempocats.pspimport.ImportDAO$$EnhancerBySpringCGLIB$$1883cf82.removeTempoAccounts(<generated>)
at ch.post.pf.jira.tempocats.pspimport.PspImport.run(PspImport.java:32)
at ch.post.pf.jira.tempocats.pspimport.Application.main(Application.java:20)

我的配置有什么问题?

最佳答案

@Transactional
void removeTempoAccounts() {

方法具有默认可见性。因此代理机制没有激活!
更改为公开后,一切都按预期工作!

关于spring-boot - Spring Boot : javax. persistence.TransactionRequiredException:执行更新/删除查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25057059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com