简体   繁体   中英

(JPQL) Retrieve all entities which are not in @OneToMany property

How can I get all Verses which are not in Receivers usedVersed property ?

Tried something like

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

But I get:

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

Here my classes:

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

UPDATE: I altered my entity classes to:

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


class Receiver{
private Long Id;
...
}

and tried this JPQL

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

Get no error, but also not a correct solution. No user is retrieved.

Try with just v , not v.id and use NOT MEMBER OF instead of NOT IN

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

You cannot select an attribute that represents a collection, which is the case with usedVerses, you have to join on your collection attribute and assign it an alias which will refer to an item from that collection, then you can select that alias. In your case it would be: select uv from Receiver r join r.usedVerses uv where r = :_receiver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM