File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Clarification needed in usuage of Example criteria. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Clarification needed in usuage of Example criteria." Watch "Clarification needed in usuage of Example criteria." New topic

Clarification needed in usuage of Example criteria.

Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Hi All,
I tried to use example criteria. For discussion sake, Lets have a entity Person

class Person
int id;
int age;
String name;
// has respective getters and setters.

If I use example query with an instance of Person with age set in it. I could fetch the list of Person with the same age. But in the case of an instance of the Person with name set in it, Hibernate does not returns a empty list. If I have set both age and name, again it works fine.

please let me know, If example criteria in Hibernate cant be used with setting a string field alone or any other non primitive type alone.

Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
Try switching on the logging and get the query being generated.
Then post the query here.

Thank you,
Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Case 1 :

Code that was used.

Result result = new Result();

session = HibernateUtil.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();

List<Result> results = session.createCriteria(Result.class)
.add( Example.create(result) )

System.out.println("Size of the result is :: "+ results.size());

for(Result result2 : list){

Hibernate Logs.

Hibernate: select this_.STUD_ID as STUD1_2_0_, this_.COURSE_ID as COURSE2_2_0_, this_.attemptNumber as attemptN3_2_0_, this_.score as score2_0_, this_.result as result2_0_ from RESULT this_ where (this_.score=? and this_.result=?)
Size of the result is :: 0

Not sure why does a where clause for the score is generated.
There is a row in the DB with result set as true.
It works if I dont comment the result.setScore(90);

Case 2 :

Code that was used

session = HibernateUtil.getSessionFactory().getCurrentSession();
trx = session.beginTransaction();

Person person = new Person();

Criteria criteria3 = session.createCriteria(Person.class)

List<Person> persons = criteria3.list();
System.out.println("Size :: "+ persons.size());

for(Person person1 : persons){
System.out.println(" f name :: " + " " + person1.getId()+" "+person1.getFirstname());

Hibernate Logs.

Hibernate: select this_.PERSON_ID as PERSON1_2_0_, this_.age as age2_0_, this_.firstname as firstname2_0_, this_.lastname as lastname2_0_ from PERSON this_ where (this_.age=? and this_.lastname=?)
Size :: 0

Same as above

Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210's what i think could be the problem..

I think score and age fields are defined as "int" and not "integer" in your hbm as well as your pojo...because of which Hibernate seems to always put them in the query with default value as 0.

If this is the case, make them as Integer(in both hbm and pojo) and it might work...

P.S. I am not sure this will work, only a little speculation, since i dont have your pojos and hbms...and i have not worked much with criteria....but made a simple hbm and pojo, with two fields, one defined as int and other as integer..Hibernate seems to always put in the int field in the where clause while using criteria....

Rahul Babbar
Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Let me try this, in the mean time, will this not be a issue if hibernate is going to assume the default values for primitives.
Does this mean Hibernate encourages wrapper object instead of primitives. (This is really bad.)

Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210 i said...i m myself only trying...

but i tried creating a pojo and hbm with a field as tried saving the entity...

the entity was saved with that field value as 0.

To me, it seems reasonable enough...

How does hibernate knows whether what is the value for a field that you have set??

Obviously, it does <pojo.getfieldName()>, which if it was an int, returns 0, and not null, since its primitive...

Now, i dont expect hibernate to know that whether the 0 is set by user or is set by the default of the int...because it has not just gets 0.

This way, makes me believe that Hibernate should encourage Wrappers and not primitives, unless your field's default is same as your primitive's default...

and i dont see anything "really bad" in it....i think its expected...

P.S. All these are only my speculations, and may be wrong...
I agree. Here's the link:
subject: Clarification needed in usuage of Example criteria.
It's not a secret anymore!