0

Error: javax.persistence.TransactionRequiredException: Executing an update/delete query

According to the documentation https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries creating the method of alteção in a repository interface it is necessary to specify the annotation @Modifying

But the same mistake continued. Looking at the documentation, https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactional-query-methods it says if I want to allow the methods of the repository interface to be transactional, just specify the @Transaction annotation.

But the same error is still occurring

Service

import javax.transaction.Transactional;

@Stateless
@Transactional
public class UsuarioService {

    @Inject 
    private IRepositorioUsuario usuarioRepositorio;

    public void updateRedifinirSenha(Long codigoPessoa, String senha) {
        usuarioRepositorio.updateRedifinirSenha(codigoPessoa, senha);
    }
}

Repository

import javax.transaction.Transactional;

@Repository
public interface IRepositorioUsuario extends JpaRepository<Usuario, Long> {

    @Modifying
    @Transactional
    @Query("update Usuario u set u.senha = :senha where u.codigoPessoa = :codigoPessoa")
    void updateRedifinirSenha(@Param("codigoPessoa") Long codigoPessoa, @Param("senha") String senha);  
}

Getting persistence context

Spec: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpd.misc.cdi-integration

public class CdiConfig {

    @Produces
    @Dependent
    @PersistenceContext(unitName = "sgr-pu")
    public EntityManager entityManager;
}

I changed the Transactional to the Transaction of the package org.springframework.transaction.annotation.Transactional, but the exception is the same.

I am using the Spring Data module in a Java EE project.

Cause By Error

Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:54)
    at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.executeUpdate(QueryNonTxInvocationDetacher.java:80)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:242)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy561.updateRedifinirSenha(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:38)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
    at com.sgr.repositorio.IRepositorioUsuario$JpaRepository$570520021$Proxy$_$$_WeldClientProxy.updateRedifinirSenha(Unknown Source)
    at com.sgr.service.UsuarioService.updateRedifinirSenha(UsuarioService.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
    at org.jboss.as.weld.ejb.DelegatingInterceptorInvocationContext.proceed(DelegatingInterceptorInvocationContext.java:87)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContext.interceptorChainCompleted(WeldInvocationContext.java:98)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContext.proceed(WeldInvocationContext.java:117)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInCallerTx(TransactionalInterceptorBase.java:129)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:55)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:76)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:47)
    at sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:74)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContext.invokeNext(WeldInvocationContext.java:83)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContext.proceed(WeldInvocationContext.java:115)
    at org.jboss.weld.bean.InterceptorImpl.intercept(InterceptorImpl.java:108)
    at org.jboss.as.weld.ejb.DelegatingInterceptorInvocationContext.proceed(DelegatingInterceptorInvocationContext.java:77)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.delegateInterception(Jsr299BindingsInterceptor.java:68)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:80)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)
    ... 141 more
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ger
  • 583
  • 2
  • 13
  • 35
  • suppose you try with adding `@Modifying(clearAutomatically = true)` in your update query. this [post](http://stackoverflow.com/questions/22533399/why-do-i-not-see-changes-issued-through-an-update-query-with-spring-data-jpa) explains more about this. – Rajith Pemabandu May 22 '17 at 00:10
  • Thanks @Rajith Pemabandu I added clearAutomatically to true, but the same exception occurred. – Ger May 22 '17 at 00:22
  • Can you provide how you are configuring your data source, entity manager, transaction manager, etc? – Sean Carroll May 22 '17 at 00:29
  • Yes of course. I edited my question by adding Getting persistence context. Thanks @SeanCarroll – Ger May 22 '17 at 00:37
  • What does "sgr-pu" actual refer to? Are you using a persistence.xml file? – Sean Carroll May 22 '17 at 00:45
  • Yes we are. It is a Java EE project – Ger May 22 '17 at 00:50

1 Answers1

0

I solved my problem as follows. Declaring a DAO and executing a hql to update. In the following post Why do I not see changes issued through an update query with Spring Data JPA? left by @Rajith Pemabandu, there is a response from @Oliver Gierke stating that these types of methods are suitable for mass changes. So basically I was breaking the pattern of development. But if anyone knows the reason for the error, share here, I will devote myself to find out the reason, but for now I am using this solution below. Thank you!

Solution

public class UsuarioDAO {

    @PersistenceContext
    public EntityManager entityManager;

    public void redifinirSenha(Long codigoPessoa, String senha) {
        Query query = entityManager.unwrap(Session.class).createQuery("update Usuario u set u.senha = :senha where u.codigoPessoa = :codigoPessoa");
        query.setLong("codigoPessoa", codigoPessoa);
        query.setString("senha", senha);
        query.executeUpdate();
    }
}
Community
  • 1
  • 1
Ger
  • 583
  • 2
  • 13
  • 35