Stivaletto primaverile: javax.persistence.TransactionRequeredException: Esecuzione di una query di aggiornamento / eliminazione

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

  •  21-12-2019
  •  | 
  •  

Domanda

Sto usando Moving Boot e configurato la mia applicazione come questa:

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

    @Autowired
    private ImportDAO importDao;

}
.

ImportDao sembra così:

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

Ma quando RemoveTeTempoacconts viene eseguito ottengo:

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)
.

Cosa c'è di sbagliato nella mia configurazione?

È stato utile?

Soluzione

@Transactional
void removeTempoAccounts() {
.

Metodo ha avuto visibilità predefinita.Pertanto il meccanismo proxy non era attivo! Dopo il passaggio a tutto il pubblico funziona come previsto!

Altri suggerimenti

Prova a utilizzare l'annotazione @Transactional dal pacchetto di primavera come @org.springframework.transaction.annotation.Transactional. Quindi dovrebbe funzionare

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top