This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object filtering in Hibernate from Database

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great...

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

Thanks Cameron Wallace McKenzie
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic