-->
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.  [ 4 posts ] 
Author Message
 Post subject: Nullable fields in criteria retrieves no value
PostPosted: Mon Jun 12, 2006 6:09 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi there,

I've got the following problem:
When I'm making a query using 'Criteria' and 'Restrictions', some objects are not retrieved at all , when a criteria field is null.
For example:

in a table EMPLOYEE (name, age, salary), I want to select an emplyee called 'Jean' (who exists), but 'salary' is null.

-----
List emps = session.createCriteria(Employee.class)
.add( Restrictions.eq("name",<name.property>) )
.add( Restrictions.eq( "salary", <salary.property> ) )
.list();
-----
But 'emps' remains an empty list.
And when I use following code:

-----
List emps = session.createCriteria(Employee.class)
.add( Restrictions.eq("name",<name.property>) )
.add( Restrictions.or(Restrictions.eq( "salary", <salary.property>), Restrictions.isNull(<salary.property>))) )
.list();
-----

All employees having a null salary are also retrieved.
Can anyone help? Thanks !!!


My environment:

Hibernate version: 3.0.5

Name and version of the database you are using: Oracle9i Enterprise Edition Release 9.2.0.5.0




Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 2:56 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Hibernate wants you to check the null values. If the values are null then don't add it to the criteria.

i.e)
Criteria criteria = session.createCriteria(Employee.class)
if(<name.property> !=null)
criteria.add( Restrictions.eq("name",<name.property>) )
if(<salary.property> !=null)
criteria.add( Restrictions.eq( "salary", <salary.property> ) )
List emps = criteria.list();

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 5:03 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi ramnath,

thanks for your reply.
The problem is not that the java property is null (it's not), but that DB content is null.
I hope you know what I'm talking about.
Thanks for your help


ramnath wrote:
Hi,

Hibernate wants you to check the null values. If the values are null then don't add it to the criteria.

i.e)
Criteria criteria = session.createCriteria(Employee.class)
if(<name.property> !=null)
criteria.add( Restrictions.eq("name",<name.property>) )
if(<salary.property> !=null)
criteria.add( Restrictions.eq( "salary", <salary.property> ) )
List emps = criteria.list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 2:46 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

I suppose you would have used SQL before for inserting the values in the table. Hibernate will not insert null values in the DB.

probably you will have to delete the null values from the DB.

I suppose you should have got a CGLIB error if you had a null values on the load query.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


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