在 Spring Data Query 中过滤子对象 [英] Filter child object in Spring Data Query

查看:15
本文介绍了在 Spring Data Query 中过滤子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下领域模型:

Playlist -> List -> Video

@Entity
class Playlist{
   // id, name, etc
   List<PlaylistItem> playlistItems;
   // getters and setters
}


@Entity
class PlaylistItem{
   // id, name, etc.
   Video video;
   // getters and setters
}


@Entity
class Video{
   // id, name, etc.
   boolean isDeleted;
   // getters and setters
}

还有我的仓库:

public interface PlaylistRepository extends JpaRepository<Playlist, Long> {
   List<Playlist> findAll();
}

现在,我如何返回仅包含现有视频的播放列表,即,如果数据库中有三个视频分配给该播放列表项目,并且其中一个视频的 isDeleted 设置为 true,那么我只需要获取两个项目相反.

Now, how do I return a playlist with only existing videos, ie, if there are three videos in the database assigned to that playlist item and one of those videos has isDeleted set to true, then I need to get only two items instead.

推荐答案

你所要做的就是在你的 PlaylistRepository 接口上声明这个方法:

All you have to do is declare this method on your PlaylistRepository interface:

List<Playlist> findByPlaylistItemsVideoIsDeleted(boolean isDeleted);

然后这样称呼它:

playListRepository.findByPlaylistItemsVideoIsDeleted(false);

这将返回包含未删除视频的所有播放列表.

That will return all playlist with videos that are not removed.

这篇关于在 Spring Data Query 中过滤子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆