Issue
I have a Quarkus application where we use Hibernate ORM with Panache to build and query the database. In some situations, we want to use an List or rather a Set to filter from a table of "requests". The Request entity, which has a different name in practice, has a status property, which is an enum that can have three values: PENDING, APPROVED or DENIED. In the web front-end, we want to use a checkbox-style filter which then during the HTTP request sends this as an array to the Quarkus application, where we then want to give it to Hibernate somehow, preferably as a Set to easily filter duplicates.
I've done something extremely similar within the NodeJS/MongoDB ecosystem in the past, which looks like this, as a step of a bigger aggregate pipeline:
aggregatePipeline.push({
$match: {
status: {
$ne: status //Array of strings
}
}
});
How would something like this be done within Hibernate? I've tried some googling, but the results are largely cluttered by people asking how you get an Arraylist out of the cursor from a standard find-query.
Thanks in advance.
Edit: Trying this line
List<Publisher> publishers = Publisher.find("name", Arrays.asList("Books", "Publishing")).list();
Gives this error:
org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = record Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Solution
Nevermind. I tried thinking about another search term which wouldn't result in a flood of irrelevant results I mentioned. I tried "Hibernate find in set", which led me to find this other post. This led me to try this line (with the Set values hardcoded for FAAFO purposes):
List<Publisher> publishers = Publisher.find("name IN ?1", new HashSet<>(Arrays.asList("Indiana University Press", "Harvard University Press"))).list();
No errors, and returns two entries that were imported into the dev db through the import.sql file. It's the "IN ?1" part that does it, not that I used a Set for my query instead of a List this time. It works just as well with a List, as long as the "IN ?1" stays there.
Answered By - Saddex
Answer Checked By - Pedro (JavaFixing Volunteer)