-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using Criteria
PostPosted: Fri Apr 07, 2006 8:23 am 
Newbie

Joined: Tue Mar 21, 2006 4:25 am
Posts: 13
Hello again,

I am now trying to do the search using the Criteria class. What I would like to know is how can I search into other tables associated with the class I want to return.

I understand that when I put this code: "Criteria searchCriteria = getSession().createCriteria(BaseStudent.class);" it means that when I call the .list() method it will return a list of BaseStudent.

Now BaseStudent has a class of type Class. how can I add a Criterion (or what should I add?) so that I say Where Class.Name = "soandso"?

reg and many thanks,
kcorp


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 11:09 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Assuming that BaseStudent contains a property "class" that is the association to "Class" then

Criteria crit = getSession().createCriteria(BaseStudent.class).createAlias("class", "c").add(Restrictions.eq("c.name", "soandso");

Curtis ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 11:47 am 
Newbie

Joined: Fri Apr 07, 2006 10:52 am
Posts: 17
Hi,

according to my experience, the Criteria class is generally only to be used for very simple searches such as first-level searches on the object fields, not for sub-object fields. Please make sure that you use HQL for more complex searches or you might find "unable to resolve property" errors.

Criteria are really easy to use:

getSession().createCriteria(MyClass.class).add(Restriction).add(Restriction)..

Restrictions are all subclasses of the Restrictions interface. The most common are Restrictions.eq(propertyName, propertyValue), Restrictions.gt(propertyName, propertyValue) and so on.

For example: this retrieves all the studens with last name equal "Doe":

ArrayList <Student> students = (ArrayList<Student>) getSession().createCriteria(Student.class).add(Restrictions.eq("lastName", "Doe").list();

The following gets students whose last names begin with Doe and whose age is more than 24 (years):

ArrayList <Student> students = (ArrayList<Student>) getSession().createCriteria(Student.class).add(Restrictions.ilike("lastName", "Doe%").add(Restrictions.gt("age", 24).list();

Of course, the Student class must have a lastName and age fields (and possibily the appropriate getters and setters).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.