Spring Data JPA中@Modifying方法的返回值是什么意思

Sha*_*jun 10 spring spring-data spring-data-jpa

@Repository
public interface LoginDao extends JpaRepository<LoginEntity, Integer> { //}, LoginDaoCustom {
    LoginEntity findByLogin(String login);

    @Modifying
    int changePassword(String password, String login);
}
Run Code Online (Sandbox Code Playgroud)

如果我将changePassword的返回值更改为int以外的任何值,我会得到以下错误.

Caused by: java.lang.IllegalArgumentException: Modifying queries can only use void or int/Integer as return type!
    at org.springframework.util.Assert.isTrue(Assert.java:65)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.<init>(JpaQueryExecution.java:166)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.getExecution(AbstractJpaQuery.java:106)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:86)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:337)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    ... 46 more
Run Code Online (Sandbox Code Playgroud)

这个整数返回值是什么意思?我确信春天有记录在某处,但我找不到它.它未列在jpa.modifying-queries中

我应该补充一点,如果将返回类型声明为int,则更新将以静默方式失败,并且不会更新值.

geo*_*and 24

int/Integer返回值是数据库中更新的行数.

  • 是的,这是 JDBC 行为。查看此文档 http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 尝试 Spring Data JPA 代码,如果一切顺利,您将看到相同的行为 (2认同)
  • 顺便说一句,您的方法需要一个@Query 才能使其工作! (2认同)