在Spring Data Query中过滤子对象 [英] Filter child object in Spring Data Query
本文介绍了在Spring Data Query中过滤子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下域名模型:
播放列表
- > 列表< PlaylistItem> ;
- > 视频
Playlist
-> List<PlaylistItem>
-> 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();
}
现在,如何返回仅包含现有视频的播放列表,即if分配给该播放列表项目的数据库中有三个视频,其中一个视频的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屋!
查看全文