Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object filtering in Hibernate from Database

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic