JasminDan
JasminDan

Reputation: 643

Spring data JPA method name query

For example a user repository,

interface PersonRepository extends Repository<User, Long> {      
   List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname); 
}

Is there a way to achieve findByEmailAddressAndLastname(null, "lastname") = findByLastname("lastname"), because after I add more parameters in user entity, the number of method name query increase exponentially. I want to reduce the redundancy. Thanks.

Upvotes: 1

Views: 658

Answers (1)

lucsbelt
lucsbelt

Reputation: 503

Since spring-data-jpa 2 you can use @Nullable annotation:

   List<Person> findByEmailAddressAndLastname(@Nullable EmailAddress emailAddress, String lastname); 

Check nullability annotations to see all the variants

Upvotes: 1

Related Questions