乐趣区

关于jpa:jpa自定义修改

当执行须要自定义批改得时候

@Modifying//update 和 delete 的时候必须标
@Query(value = "update mic_line set rod_no=?1 where line_no=?2",nativeQuery = true)
void updateRod(String rodNo,String lineNo);

Spring JPA 报错:InvalidDataAccessApiUsageException: Executing an update/delete query
集成 Spring JPA,在执行 delete,update 时须要增加事务。

错误信息:

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:402)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

解决:通过增加注解 @Transactional

@Transactional
@Modifying//update 和 delete 的时候必须标
@Query(value = "update mic_line set rod_no=?1 where line_no=?2",nativeQuery = true)
    void updateRod(String rodNo,String lineNo);

留神,增加的是 import org.springframework.transaction.annotation.Transactional。

退出移动版