I have a JPA repository interface with the following method:
@Modifying
@Query(value = "DELETE FROM bivrip.bloop_options o WHERE o.bloop_id = ?1 AND o.id not in ?2", nativeQuery = true)
public void removeOtherOptions(int bloopID, List<Integer> validOptionIDs);
When the validOptionIDs
contains at least one element, the method behaves as expected and removes all ids not in the list associated with the given bloop. However, if the list is empty, it doesn’t delete anything – when I would assume it would delete all options for the provided bloop.
Do I need to do anything extra configuration-wise in order to make this work? Or am I condemned to writing another method along the lines of "removeAllOptionsForBloop"?
Note: If this should generally work and is a result of particular database implementation, I’m using postgres.
2
Answers
Rewrite the query to use an array parameter and the
<> ALL
operator:That will work for empty arrays as well.
You can try to use JpaSpecificationExecutor if you like, I think It is better because you have more control of your query and you don’t need to write native SQL.