【问题标题】:Spring Boot: javax.persistence.TransactionRequiredException: Executing an update/delete querySpring Boot:javax.persistence.TransactionRequiredException:执行更新/删除查询
【发布时间】:2014-09-23 06:56:49
【问题描述】:

我正在使用 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)

我的配置有什么问题?

【问题讨论】:

  • 你可以尝试制作一个ImportDAO 将实现的接口并告诉我会发生什么吗?
  • 或者尝试将cglib添加到您的构建文件中。

标签: spring-boot


【解决方案1】:
@Transactional
void removeTempoAccounts() {

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

【讨论】:

    【解决方案2】:

    尝试使用Spring包中的@Transactional注解 喜欢@org.springframework.transaction.annotation.Transactional 然后它应该工作

    【讨论】:

      猜你喜欢
      • 2021-07-04
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2014-11-07
      • 2012-09-01
      • 2013-04-18
      • 1970-01-01
      • 2019-07-16
      相关资源
      最近更新 更多