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);
}
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.
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.
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.
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
andNotIn
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With