I have a table that looks like this:
id | A | B |
---|---|---|
1 | A1 | B1 |
2 | A1 | B2 |
3 | A2 | B2 |
4 | A2 | B3 |
Then I have a list of items for which I want to find the corresponding database rows. For instance, i might have (in the form of JSON)
[
{
"A": "A1",
"B": "B4",
},
{
"A": "A1",
"B": "B2",
}
]
This should now return the rows with Id 2. These items for which I want to query the database can be thousands.
Naively, I came up with
findAllByAinAndBIn(List<A> aValues, List<B> bValues)
but that doesn’t work since this doesn’t find the rows with a nth element to nth element comparison. What should i do instead?
2
Answers
The simplest approach would be to create a concatenated list of ids and then compare them against the database using a JPQL query.
1. Create a concatenated list of the ids
2. Create a JPQL query to compare the concatenated ids against the database
The following query demonstrates an approach using a JSON array of search terms:
The following is an example using a JPA native query: