Démarrage de printemps :javax.persistence.TransactionRequiredException :Exécuter une requête de mise à jour/suppression

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

  •  21-12-2019
  •  | 
  •  

Question

J'utilise Spring Boot et j'ai configuré mon application comme ceci :

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

    @Autowired
    private ImportDAO importDao;

}

ImportDAO ressemble à ça :

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

Mais lorsque RemoveTempoAcconts est exécuté, j'obtiens :

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)

Quel est le problème avec ma configuration ?

Était-ce utile?

La solution

@Transactional
void removeTempoAccounts() {

Méthode avait une visibilité par défaut.Par conséquent, le mécanisme de proxy n'était pas actif! Après avoir changé dans le public, toujours des œuvres comme prévu!

Autres conseils

Essayez d'utiliser le package @Transactional Annotation from Spring comme @org.springframework.transaction.annotation.TransactionalAlors ça devrait marcher

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top