Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does spring CrudRepository support deleteBy of a list?

Does the Spring Data CrudRepository provide deletion of a list of entries by a attribute that is not the primary key?

public interface MyRepository extends CrudRepository<MyEntity, Long> {
    @Modifying
    @Transactional
    public void deleteByName(List<String> names);
}
like image 477
membersound Avatar asked Jan 21 '15 09:01

membersound


People also ask

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is the use of CrudRepository in Spring boot?

CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database.

What is true about CrudRepository and JpaRepository in Spring data JPA?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.


1 Answers

Yes this is possible, the documentation explains the In keyword with the following example. It is further shown by the example that the list argument does not have to be of the type of the primary key of the entity:

In -> findByAgeIn(Collection<Age> ages)

In and NotIn also take any subclass of Collection as parameter as well as arrays or varargs.

and this in turn could then be applied to your delete query:

In addition to query methods, query derivation for both count and delete queries, is available.

Something like:

void deleteByNamesIn(List<String> names);
like image 87
reto Avatar answered Sep 21 '22 09:09

reto