Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data: can it find by two values of the same field without writing the implementation?

I am doing a Spring web app and I use Spring Data.

I am able to use Spring Data to find objects by a single value of a field. For example:

some_object_repository.findByFirstName("John") 

Is there any way I can provide two first names (e.g., "John", "David") similar to the following in concept:

some_object_repository.findByFirstName({"John", "David"})

without me writing a custom implementation?

Regards and thanks!

like image 577
curious1 Avatar asked May 28 '14 04:05

curious1


People also ask

What does @repository do in Spring?

Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.

How do you add multiple or conditions to Spring data?

Please refer the following code : Criteria criteria = new Criteria(); criteria. andOperator(Criteria. where("siteCode").is(siteCode)); if(paymentMode !=

Can we add multiple data sources in Spring application?

So, to use multiple data sources, we need to declare multiple beans with different mappings within Spring's application context.

What is query method in Spring data?

Query methods are methods that find information from the database and are declared on the repository interface. Spring Data has pretty versatile support for different return values that we can leverage when we are adding query methods to our Spring Data JPA repositories.


1 Answers

You can do this with In at the end

findByAgeIn(Collection ages) … where x.age in ?1

http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods

Section 2.3.2 Query creation

In your case it will be

findByFirstNameIn(Collection names)

like image 128
mavarazy Avatar answered Nov 15 '22 21:11

mavarazy