Spring data deleteBy query doesnt return deleted object

∥☆過路亽.° 提交于 2020-08-08 06:37:53

问题


In Spring-data-mongodb, can we return the single deleted object from query method of repository like below

public interface MyRepository extends MongoRepository<MyObject, String>{
 Optional<MyObject> deleteByXAndY(String x, String y);
}

if there will be always single document that's get deleted by above query.

I tried it but it throws exception like cant convert Long to MyObject. I think only void, long or List or Stream are supported. Is there any way to achieve what I am trying to do?


回答1:


Spring doesn't know that only one object can ever be deleted by this method, so it won't allow you to define it with a single result returned. After all, there is no guarantee in the "contract" you have defined here that there will only be one match. You should probably define the method to return a List and then just get the first object from the list if you are sure there will only be one.



来源:https://stackoverflow.com/questions/57076537/spring-data-deleteby-query-doesnt-return-deleted-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!