Querying ManyToMany relationship [message #650062] |
Fri, 21 January 2011 10:20  |
Eclipse User |
|
|
|
Hello,
we have a MedaItem class, which has a bidirectional ManyToMany relationship to the Genre class.
MovieItem and SeriesItem derives from MediaItem.
How can I query this relationship to get only the MovieItems or only the SeriesItems with a specific genre?
And: which type of query is the "best" therefore? NamedQuery, Criteria Query...
Are there queries in a form like this "select item from MovieItem item where item.genres CONTAINS :genre" ?
Thanks
Dirk
[Updated on: Fri, 21 January 2011 10:23] by Moderator
|
|
|
Re: Querying ManyToMany relationship [message #650362 is a reply to message #650062] |
Mon, 24 January 2011 11:03  |
Eclipse User |
|
|
|
Hello Dirk,
JPQL queries on a class will bring in all subclass instances as well. So a query on MediaItem will bring in MovieItem and SeriesItems as well unless the TYPE operator is used to filter for a particular set of class types. You can also query directly on a subclass directly to exclude its parent/sibling classes ie:
"Select item from MovieItem item, IN(item.genres) genre where genre = :genre"
Or both MovieItem and SeriesItems:
"Select item from MediaItem item, IN(item.genres) genre where genre = :genre and TYPE(item) in (MovieItem, SeriesItem)"
Specifing IN within the select clause is not required ,as you can probably use MEMBER OF function on the item.genres as well, ie ":genre MEMBER OF item.genres"
Best Regards,
Chris
|
|
|
Powered by
FUDForum. Page generated in 0.05527 seconds