Reputation: 75
I have ID array type. ex) Long id = [1, 3, 5] Then how to find the data use array type id?
Array data type not applicable to findById() method. So i tried loop function. But this style is not good to performance Because it is execute the method several times for id's length.
Long id = [1, 3, 5];
for (int i = 0; i < id.length; i++) {
repository.findById(id[i]);
}
Board Table
id | title | content
==============================
1 | A... | A is...
2 | B... | B is...
3 | C... | C is...
4 | D... | D is...
5 | E... | E is...
I want same result use JPA method(NOT Native Query Style) below query.
SELECT title, content FROM Board WHERE id = 1, or id = 3, or id = 5;
Result
id | title | content
==============================
1 | A... | A is...
3 | C... | C is...
5 | E... | E is...
Upvotes: 3
Views: 2388
Reputation: 1430
If you use CrudRepository
you can use the method findAllById(Iterable<ID> ids)
to find some data
String[] ids = new String[]{"1","2","3"};
Iterable<T> result = repository.findById(Arrays.asList(ids))
Upvotes: 3
Reputation: 1819
You can use the method findByIdIn(List<Long> ids)
.It is the Spring data JPA method to get the data.
Your method should be
List<Board> findByIdIn(List<Long> ids)
in BaordRepository
Upvotes: 2