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

    @Modifying
    int changePassword(String password, String login);
}

If I change the return value of the changePassword to anything other than int, I get the error below.

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

What does this integer return value mean ? I am sure spring has it documented somewhere, but I can't find it. It is not listed in the jpa.modifying-queries

I should add that if do declare the return type as int, the update fails silently and values aren't updated.

1
  • Can somebody elaborate on the negative vote please ? Apr 2, 2014 at 17:34

1 Answer 1

48

The int/Integer return value is the number of rows that where updated in the database.

6
  • TY, is this a basic JDBC thing ? Would you kindly point me to some documentation ? Apr 2, 2014 at 17:47
  • 3
    Yes, this is JDBC behavior. Check out this documentation docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html Try the Spring Data JPA code and you'll see the same behavior if all goes well
    – geoand
    Apr 2, 2014 at 17:49
  • 2
    By the way, you will need a @Query on your method in order to get it to work!
    – geoand
    Apr 2, 2014 at 17:55
  • Oh, I had the @NamedQuery in the Entity. Method returns 1 but for some reason the record returned by findAll does not reflect the changes. I would have expected some error. Apr 2, 2014 at 18:06
  • 1
    Maybe you should try some of the options of @Modifying
    – geoand
    Apr 2, 2014 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.