spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Add support for deleteBy/removeBy queries

Open Thunderforge opened this issue 4 years ago • 5 comments

Since 2014, core Spring Data JPA has supported deleteBy/removeBy in queries, just like findBy queries. However, Spring Data Redis does not support this, instead throwing

java.lang.UnsupportedOperationException: Query method not supported.
	at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.doExecute(KeyValuePartTreeQuery.java:144)
	at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.execute(KeyValuePartTreeQuery.java:110)
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:159)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at com.sun.proxy.$Proxy115.deleteByRecordingSid(Unknown Source)

I would like to request that deleteBy (and its synonym removeBy) be supported in queries.

Code Example

@RedisHash("Student")
public class Student {

    @Id
    private String id;
    @Indexed
    private String name;

    // Getters and setters
}
@Repository
public interface StudentRepository extends CrudRepository<Student, String> {

    Student findByName(String name);

    void deleteByName(String name);
}
studentRepository.findByName("John Doe"); // Successful
studentRepository.deleteByName("John Doe"); // UnsupportedOperationException

Thunderforge avatar Aug 24 '21 19:08 Thunderforge

Hi Thunderforge,

Please help me, I am facing deleteBy query above issue. Please do let me know how to resolve this. I have tried both deleteBy/removeBy. Getting same exception " Query method not supported" My table is Product having List of Brand (another table) attribute. I have created method as deleteByBrands_Id/removeByBrands_Id public class Product { @Id @Indexed private String id; @NonNull @Indexed private List<Brand> brands;

Note: Working fine for existsByBrands_Id/findByBrands_Id

siddubai avatar May 05 '23 06:05 siddubai

@siddubai The reason I created this feature request is because spring-redis does not support this functionality.

The workaround is to do findByBrands() or whatever and then delete it. Definitely suboptimal, but until the functionality for a deleteByBrand() is implemented, that's all we can do.

Thunderforge avatar May 05 '23 20:05 Thunderforge

@Thunderforge Thank you so much for your quick update :) Sorry to ask you, Did not get, using workaround(findByBrands) how can I delete it? Please elaborate.

siddubai avatar May 05 '23 23:05 siddubai

@siddubai A delete() method is automatically generated by Spring when you use CrudRepository. What I'm saying is to do:

var product = productRepository.findByBrand("Brand X");
productRepository.delete(product);
// productRepository.deleteByBrand("Brand X"); // Doesn't work due to UnsupportedOperationException

Thunderforge avatar May 05 '23 23:05 Thunderforge

@Thunderforge Now I got it, what you are trying to say. But my requirement is not to delete complete product. want to delete only one of the brand in product. (Similarly there are many nested objects.) Still is there any alternate workaround please do let me know. However thank you so much for your precious support.

siddubai avatar May 05 '23 23:05 siddubai