Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Querying ManyToMany relationship
Querying ManyToMany relationship [message #650062] Fri, 21 January 2011 15:20 Go to next message
dreske Missing name is currently offline dreske Missing nameFriend
Messages: 17
Registered: August 2010
Junior Member

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" ?


[Updated on: Fri, 21 January 2011 15:23]

Report message to a moderator

Re: Querying ManyToMany relationship [message #650362 is a reply to message #650062] Mon, 24 January 2011 16:03 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1342
Registered: July 2009
Senior Member
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,
Previous Topic:[Fixed] NPE when using LAZY loading on ManyToOne relationship
Next Topic:Getting a JDBC Connection from an EntityManager returns null in EclipseLink 2.0.2
Goto Forum:

Current Time: Mon Mar 30 14:48:57 GMT 2020

Powered by FUDForum. Page generated in 0.02404 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top