Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crudrepository findBy method signature with multiple in operators?

I have an Entity Class like this:

@Entity @Table(name = "EMAIL") class Email{     @Id     @Column(name = "Id")     Long id;     @Column(name = "EMAIL_ID")     String emailId;     @Column(name = "PIN_CODE")     String pincode; } 

How to write findBy method for the below query using crudrepository spring data jpa?

select email_id,name from email_details where eamil_id in('[email protected]','[email protected]') and pin_code in('633677','733877') 

I am expecting the spring data jpa method like the below but how to construct it?

List<Email> findBy.....(List<String> emails, List<String> pinCodes); 

I want to get list of Emails in a single database hit.

like image 793
Venkata Rama Raju Avatar asked Sep 26 '15 10:09

Venkata Rama Raju


People also ask

Which is better CrudRepository or JpaRepository?

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.

What is the difference between a CrudRepository and a JpaRepository What is the difference between a CrudRepository and a JpaRepository?

CrudRepository extends Repository interface whereas JpaRepository extends PagingAndSortingRepository and QueryByExampleExecutor interface. PagingAndSortingRepository further extends CrudRepository only.


1 Answers

The following signature will do:

List<Email> findByEmailIdInAndPincodeIn(List<String> emails, List<String> pinCodes); 

Spring Data JPA supports a large number of keywords to build a query. IN and AND are among them.

like image 102
Tunaki Avatar answered Oct 08 '22 20:10

Tunaki