*
The moose likes Object Relational Mapping and the fly likes Object filtering in Hibernate from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Object filtering in Hibernate from Database" Watch "Object filtering in Hibernate from Database" New topic
Author

Object filtering in Hibernate from Database

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Hi,

Ok let me explain the problem. I have a database table called "Person" with following fields ID,NAME,AGE and SEX.

Now I have corresponding JAVA Bean called Person.java with all above listed column names as attributes.

Now instead of writing a HQL for filtering data from database as
for retrieving data of Person having ID greater then 56, I am wondering, if Hibernate supports any code like e.g.



Now the above code could simply give me all the dataset according to the matching condition.

Any help would be greatly appreciated.
Thanks


The Best way to predict your future is to create it - Every great individual common man
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Absolutely!

I think what you're looking for is the Hibernate Criteria API. For example, to find all users who are verified, an example on my website, the code would look like this:



From How to Use the Criteria API: Hibernate Examples and JPA Tutorials

The Criteria API provides a variety of helpful classes, with the most important being the Restriction, and perhaps the Example, classes. I think you'll find this makes thinking about your database queries in an object oriented way very easy to do.

-Cameron McKenzie
[ May 27, 2008: Message edited by: Cameron Wallace McKenzie ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

That's great...

Yeah I was talking about the same, as you have brilliantly described.

Thanks Cameron Wallace McKenzie
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Ok Cameron,

Here comes my another question.

I have a java bean class like.


This person class has one bean as an attribute that bean is as follows.



Now I am trying to filter out Objects from database using hibernate using crtiteria API like :



But instead of applying a criteria on main_bean.attribute, I want a criteria for main_bean.event.title, i.e. applying criteria on child bean's attribute. How can I do that??

Let me know If anything hasn't been explained completely...
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Ok I got it...

answer is in my question. So If we need to access child bean's attribute, then I must address it like "child_bean.attribute" instead of "attribute"


Thanks anyways.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

This is a tried and true facilitation technique. Ignore the question until the user finds the answer themselves.

Thanks for posting back your answer!

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object filtering in Hibernate from Database