Reputation: 231
Normally deleting many records from a table at once when you have a List is pretty straightforward. Right now my query looks like this:
Query q = em.createQuery("DELETE from SomeEntity e WHERE e.id in :someEntityIds")
.setParameter("someEntityIds", someEntityIds);
where someEntityIds
is a List<int>
of id's that I want to delete.
However, I need to modify the code to delete based on two different id's. I have a model class, SomeModel
that has two variables in it
public class SomeModel {
public String someId;
public Long anotherId;
}
Note that this model does not directly correspond with my JPA model/entity. The table will have these two values, plus a bunch more.
I have a list, List<SomeModel>
, that I need to use to delete multiple records at once. In other words, I have to delete every single record that matches both someId
and anotherId
in a list of SomeModels
. Is this possible without iterating through the List and running a delete query on each item in the list?
Upvotes: 0
Views: 182
Reputation: 19565
Possibly concat
could work but you'll have to prepare a list of composite ids beforehand:
// List<SomeModel> list = ... // assuming there's a list of some models
List<String> compositeIds = list.stream()
.map(m -> m.someId + "#" + Integer.toString(m.anotherId))
.collect(Collectors.toList());
Query q = em.createQuery("DELETE from SomeModel e WHERE concat(e.someId, '#', e.anotherId) in :compositeIds")
.setParameter("compositeIds ", compositeIds);
Upvotes: 1