-->
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.  [ 9 posts ] 
Author Message
 Post subject: Could hibernate ignore null value if it have many parameter?
PostPosted: Fri Sep 10, 2004 12:27 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi everyone:

There is a search function in my web application.I want to implement the function using hibernate. But the problem is that the query parameter is too much. There are 10 parameter in this site search function. So I think it has a problem. For example:

search according the following condition:

name: age: email: address: cell_phone:
home_phone: phone: country: ... ... etc...

If the client don't input any things my method will get a "null" value or a blank value. The function will not work property. But the fact is that if user don't input anything the condition will be all. I mean that if user don't input the name parameter,the function will query all name's data.

For example:
if the input info is this:
name: yashnoo. email:yashnoo@yahoo.com

the query will be :
Code:
from mytable where name=yashnoo and email=yashnoo@yahoo.com

if the input info is this:

name: (not input anything) email:yashnoo@yahoo.com

the query will be:
Code:
from mytable where email=yashnoo@yahoo.com

Not this:
Code:
from mytable where name=null and email=yashnoo@yahoo.com

The later will query nothing obviously.

Could hibernate has any query method that can ignore null value? THks

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 2:56 am 
Regular
Regular

Joined: Mon Oct 06, 2003 7:17 am
Posts: 58
Location: Switzerland
No, I think it hasn't. Try a generic solution with the criteria API.

Reto


Top
 Profile  
 
 Post subject: :(
PostPosted: Fri Sep 10, 2004 5:45 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
The method :
Code:

session.createCriteria(Expression.between("name",null,null));

also don't work property and achieve my goal. You think need I have to check the parameter in my java code manually?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 7:08 am 
Newbie

Joined: Fri Aug 29, 2003 4:36 am
Posts: 16
Location: Belgium
Are you aware of the "query by example" functionality?
This might be useful in your situation.

http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html#querycriteria-examples


Top
 Profile  
 
 Post subject: :(
PostPosted: Sat Sep 11, 2004 7:49 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Bart Sawyer wrote:
Are you aware of the "query by example" functionality?
This might be useful in your situation.

http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html#querycriteria-examples


No,NO. It is not this means. I means that I want to insert or update data to database ,not load data from database.

_________________
You are not alone...


Top
 Profile  
 
 Post subject: :-(
PostPosted: Wed Sep 22, 2004 10:41 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
For example.

I use the query
Code:
"from cat  as c where c.name="+request.getParameter("name"); 


I mean that if I get the parameter is null ,it will query all the cat object from database , it equals this:
Code:
from cat as c


This is common problem when we design a query system. User maybe input Null value,but it means that he want to query all the object not NULL.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 12:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
No,NO. It is not this means. I means that I want to insert or update data to database ,not load data from database.

Huh? Sure sounds like your trying to load data from the database to me.

Query-by-example, as Bart mentioned, has the capabilities to do what you want.

Otherwise, yes you will need to handle this in your java code. Typically you'd manually build up an HQL string and only include restrictions based on values the user actually entered.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 1:05 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
steve wrote:
Quote:
No,NO. It is not this means. I means that I want to insert or update data to database ,not load data from database.

Huh? Sure sounds like your trying to load data from the database to me.

Query-by-example, as Bart mentioned, has the capabilities to do what you want.

Otherwise, yes you will need to handle this in your java code. Typically you'd manually build up an HQL string and only include restrictions based on values the user actually entered.


Thank you. It seems that I will implement it using "if else". Hibernate don't support this function . :-(

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 9:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sure it does; QBE supports it. Of course if you see a deficiency you can always open a jira case. And of course patches are always welcome.


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