• 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

Clarification needed in usuage of Example criteria.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

thanks
Arjun.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try switching on the logging and get the query being generated.
Then post the query here.
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case 1 :
=========

Code that was used.

Result result = new Result();
//result.setScore(90);
result.setResult(true);

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

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

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

for(Result result2 : list){
System.out.println(result2.getScore());
}


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

Comment
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();
//person.setAge(23);
person.setLastname("gopi");

Criteria criteria3 = session.createCriteria(Person.class)
.add(Example.create(person));

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

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

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

Comments
Same as above

regards
Arjun.
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...here'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....

 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)

thanks
Arjun.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well...as i said...i m myself only trying...

but i tried creating a pojo and hbm with a field as int...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 way...it 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 need a new interior decorator. This tiny ad just painted every room in my house purple.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic