Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java spring repositories - findBy() method using set of IDs/values

I have a java spring app. It does back end stuff, offering a Restful API, all using an Oracle database.

For most resources, it follows a typical design pattern: "Controller > Service > Repository > DB".

At the repository level, when extending the JpaRepository interface, one can define a set of methods without having to provide a body, just like this:

public interface SurgeryRepository extends JpaRepository<Surgery, String> {

    public List<Surgery> findByPracticeNameContainingIgnoreCase(String substring); 

    public Surgery findById(String id); 

}

What i would like to know is how to define a method that allow me to retrieve multiple rows using a list of ids, something like:

public List<Surgery> findByIDs(List<String> IDs); 

Something that should automatically map to this sort of SQL

SELECT * FROM SURGERIES WHERE SURGERY_ID IN ('101',102,103',104',105',106')

... but without having to write a native query in the java code. (and not necessarily for id, it could be for any field) Thanks in advance.

like image 232
MikaelW Avatar asked Jan 31 '16 21:01

MikaelW


Video Answer


1 Answers

You can use:

findAll(Iterable<ID> ids) 

Method provided by JpaRepository.

like image 89
eg04lt3r Avatar answered Oct 18 '22 12:10

eg04lt3r