This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Querying Based On Member of Contained Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Querying Based On Member of Contained Collection" Watch "Querying Based On Member of Contained Collection" New topic
Author

Querying Based On Member of Contained Collection

Jason Ferguson
Ranch Hand

Joined: Aug 09, 2007
Posts: 58
Okay, given the following class:



In my code, I'll create and persist Employee objects like this:



But later, I'd like to query for a particular Employee.


This doesn't work, because Restrictions.in() expects a String for the field name and a collection.

Maybe I'm overlooking something, but how can I query based on whether a certain object is contained in a persisted object's member collection?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Basically, you want to restrict based on an attribute in the related object.

So you use a Join and then you can add the joined objects attribute in the restriction.

To do a join in a Criteria object is with the addAlias() method of Criteria.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Querying Based On Member of Contained Collection
 
Similar Threads
In struts2 delete is not Working
dynamic form elements using struts
Simple requirement .. but I don't know how!
Sending collection to Action
Deleting Record in a Web Application.