Either use @Param on all parameters except Pageable and Sort typed once, or none at all




Asked on October 14, 2023

I am creating Spring Boot application using CrudRepository.


public interface UserRepository extends CrudRepository<User, Integer> { @Query(value= "INSERT INTO User (name, gender, isMarried) VALUES (:name, :gender, :isMarried)") void saveUser(@Param("name") String name, @Param("gender") String gender, Boolean isMarried); }

Getting error for @Param. Why?

Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all at org.springframework.util.Assert.isTrue(Assert.java:122) ~[spring-core-6.0.11.jar:6.0.11] at org.springframework.data.repository.query.Parameters.assertEitherAllParamAnnotatedOrNone(Parameters.java:375) ~[spring-data-commons-3.1.3.jar:3.1.3] at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:137) ~[spring-data-commons-3.1.3.jar:3.1.3] at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:73) ~[spring-data-commons-3.1.3.jar:3.1.3] at org.springframework.data.jpa.repository.query.JpaParameters.<init>(JpaParameters.java:46) ~[spring-data-jpa-3.1.3.jar:3.1.3] at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:451) ~[spring-data-jpa-3.1.3.jar:3.1.3] at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:66) ~[spring-data-jpa-3.1.3.jar:3.1.3] at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:86) ~[spring-data-commons-3.1.3.jar:3.1.3] at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:113) ~[spring-data-jpa-3.1.3.jar:3.1.3] at org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory.build(DefaultJpaQueryMethodFactory.java:44) ~[spring-data-jpa-3.1.3.jar:3.1.3] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94) ~[spring-data-jpa-3.1.3.jar:3.1.3]




Replied on October 14, 2023
Error message states clearly.

"Either use @Param on all parameters except Pageable and Sort typed once, or none at all"

It means 

1. Use @Query with @Param for all parameters.

void saveUser(@Param("name") String name, @Param("gender") String gender, @Param("isMarried") Boolean isMarried); }

2. Or with none.

void saveUser(String name, String gender, Boolean isMarried); }




Replied on October 14, 2023
Thanks. Got it.

Write Answer








©2024 concretepage.com | Privacy Policy | Contact Us