使用IN子句嵌套JPA查询

时间:2015-04-08 02:56:04

标签: java hibernate jpa many-to-many

所以我有以下java类

PLAYLIST
private long playlistid;
private String name;
private String playlistpersistentid;
private String librarypersistentid;

TRACK
private long trackid;
private String trackpersistentid;
private String name;
private String artist;
private String album;
private String genre;
private String librarypersistentid;

具有多对多关系,映射到此查找表:

PLAYLIST_TRACK TABLE
playlist_playlistid
tracks_trackid

我想写的查询是

"Select * from Track where Trackid in (Select Tracks_trackid from Playlist_Track where Playlist_playlistid = '#WhicheverIdWasEntered')

有人可以帮我用JPA编写查询吗?

编辑:我正在尝试根据播放列表ID返回特定播放列表中的所有曲目。

1 个答案:

答案 0 :(得分:1)

"Select * from Track where Trackid in (Select Tracks_trackid from Playlist_Track where Playlist_playlistid = '#WhicheverIdWasEntered')"

这不是一个SQL查询的jpql查询,试试这个

"SELECT t FROM Track t where t.Trackid IN (SELECT pt.Tracks_trackid FROM Playlist_Track pt WHERE pt.Playlist_playlistid = :WhicheverIdWasEntered)"

确保在查询中使用正确的实体名称。