Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA "OR" with single parameter

Is it possible to have a Spring Data JPA repository method like this:

User findByEmailOrUserName(String usernameOrEmail);

The above method name doesn't work as Spring Data JPA throws an exception when it tries to find the second parameter that needs to be bound to second property name in the method.

I want a method name that will translate to select t from User t where t.email = :usernameOrEmail OR t.userName = :usernameOrEmail

Does the method name queries have such a possibility?

like image 494
Mubin Avatar asked May 31 '16 21:05

Mubin


People also ask

Should I use Spring Data JPA or JDBC?

The main advantage of JPA over JDBC for developers is that they can code their Java applications using object-oriented principles and best practices without having to worry about database semantics.

What is the advantage of Spring Data JPA?

Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

What is the difference between spring data and Spring Data JPA?

Spring Data JPA is a sub-project of Spring Data and provides an abstraction over the Data Access Layer using Java Persistence API and ORM implementations like Hibernate.

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.


1 Answers

With current latest version of Spring Data JPA (v1.10.2), this is not possible. I don't see any such requests for future versions either.

like image 110
Mubin Avatar answered Oct 04 '22 21:10

Mubin