Frühlingsstiefel:javax.persistence.TransactionRequiredException:Ausführen einer Aktualisierungs-/Löschabfrage

StackOverflow https://stackoverflow.com//questions/25057059

  •  21-12-2019
  •  | 
  •  

Frage

Ich verwende Spring Boot und habe meine Anwendung wie folgt konfiguriert:

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

    @Autowired
    private ImportDAO importDao;

}

ImportDAO sieht so aus:

@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();
    }
}

Aber wenn „removeTempoAcconts“ ausgeführt wird, erhalte ich:

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)

Was stimmt mit meiner Konfiguration nicht?

War es hilfreich?

Lösung

generasacodicetagpre.

-Methode hatte eine Standard-Sichtbarkeit.Daher war der Proxy-Mechanismus nicht aktiv! Nachdem Sie sich in der Öffentlichkeit umgewandelt haben, funktioniert wie erwartet!

Andere Tipps

Versuchen Sie, die @Transactional -Annotation aus dem Frühlingspaket wie zu verwenden @org.springframework.transaction.annotation.TransactionalDann sollte es funktionieren

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top