-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Help required in Criteria API
PostPosted: Fri Jan 29, 2010 7:41 am 
Newbie

Joined: Fri Jan 29, 2010 7:27 am
Posts: 1
Hi I have my domain model like this:

class Parent{
Set child;
long parentID;
String name;
int age;
String surname;

public void addChild(Child child){}
}

Class Child{
Set grandChild;
long childID;
long parentID;
String name;
int age;
String surname;

Parent parent;

public void addGrandChild(GrandChild grandChild){}
}


Class GrandChild{

long childID;
long grandChildID;
String name;
int age;
String surname;
Child child;
}

Basically a relation with of one to many from Parent to Child and one to many from child to grandchild.

I want to get all the parent records along with the child and grand child records using Criteria API and how can I add restrictions like the child age < 45 and grand child age >5 in the Criteria query. Pls help I am new to hibernate.


Top
 Profile  
 
 Post subject: Re: Help required in Criteria API
PostPosted: Fri Jan 29, 2010 10:59 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Sounds like a good use of the Restrictions class. Here's an example of its use using a greaterthan. Just do the query on the central object, get the objects back, and you can just call the getters to get the associated objectsl

Code:
Session session = HibernateUtil.beginTransaction();
Criteria criteria =
      session.createCriteria(User.class);
criteria.add(Restrictions.gt("id", 5));
List results = criteria.list();
HibernateUtil.commitTransaction();
for (int i = 0; i<results.size(); i++) {
  System.out.println(results.get(i).toString());
}


Here's a tutorial on using the Hibernate Criteria API:

http://www.hiberbook.com/HiberBookWeb/l ... riteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.