(JPQL)检索不在@OneToMany属性中的所有实体

时间:2014-06-17 12:48:11

标签: java jpa jpql

如何获取 Versers 所有 接收器 usedVersed < / em> 属性?

尝试了类似

的内容
@NamedQuery(name="unusedVersesByReceiver", query="SELECT v.id FROM Verse v WHERE v.id 
NOT IN (SELECT r.usedVerses FROM Receiver r WHERE r = :_receiver)"

但我明白了:

The state field path 'r.usedVerses' cannot be resolved to a collection typ

这是我的课程:

class Verse{
private Long Id;
private String SomeText;
...
}
class Receiver{
private Long Id;
@OneToMany
private List<Verse> usedVerses;
...
}

更新:我将我的实体类改为:

class Verse{
private Long Id;
private String SomeText;
@OneToMany
private User usedBy;
...
}


class Receiver{
private Long Id;
...
}

并尝试了这个JPQL

@NamedQuery(name = "testUnusedVerses", query= "SELECT v.id FROM Verse v WHERE v.usedBy 
!= :_receiver")

没有错误,但也没有正确的解决方案。没有用户被检索。

2 个答案:

答案 0 :(得分:0)

仅使用v,而不是v.id,然后使用NOT MEMBER OF代替NOT IN

SELECT v.id FROM Verse v, Receiver r
    WHERE v NOT MEMBER OF r.usedVerses 
    AND r = :_receiver

答案 1 :(得分:0)

您无法选择表示集合的属性(使用usedVerses的情况),您必须加入集合属性并为其指定一个别名,该别名将引用该集合中的项目,然后您可以选择该别名。 在你的情况下,它将是: 从Receiver r join r.usedVerses uv中选择uv,其中r =:_receiver