Reputation: 1427
I have a Repository interface and name derived query method in it:
int deleteAllBySpaceIdAndUserId(UUID spaceId, UUID userId);
Calling this method results in Select query first and then Delete query.
What is the point of issuing select? Why isn't it evaluated as single query? Why do i need it to work this (weirdly) way?
Upvotes: 1
Views: 185
Reputation: 44525
Spring Data doesn't execute direct SQL queries to delete an entity, but uses the EntityManager and it's remove method.
As an example you can look in the class org.springframework.data.jpa.repository.support.SimpleJpaRepository, which provides implementations for the default methods (e.g. deleteById etc.):
public void delete(T entity) {
Assert.notNull(entity, "Entity must not be null!");
if (entityInformation.isNew(entity)) {
return;
}
Class<?> type = ProxyUtils.getUserClass(entity);
T existing = (T) em.find(type, entityInformation.getId(entity));
// if the entity to be deleted doesn't exist, delete is a NOOP
if (existing == null) {
return;
}
em.remove(em.contains(entity) ? entity : em.merge(entity));
}
Since the remove method of the EntityManager takes the entity object itself, and not just an ID value or similiar, Spring Data has to execute the find method with the chosen parameters (either ID, or in your case 2 values), to get the actual entity object and then to remove it from the EntityManager.
Upvotes: 1