-->
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.  [ 6 posts ] 
Author Message
 Post subject: Binding null parameter to query
PostPosted: Wed Oct 29, 2003 11:08 am 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Hi

I would like to execute query:

SELECT p FROM Person AS p WHERE ((? is null) OR (p.firstname = ?)) AND ((? is null) OR (p.lastname = ?))

This query assumes that person can be searched by firstname, lastname or both. Right now hibernate throws NullPointerException when I'm trying to bind null parameter because it tries to guess parameter type and ...
Maybe in Hibernate we need to introduce NullType for scalar values ? I know that is dialect specific but it would be awesome to have it.

Reason for such funcionality is to have dynamic searches from UI when user can enter bunch of search parameters and not all parameters are required.



PS:

I know that query above can be expressed by search criteria but this is just an example real query is more complicated and it can not be expressed with Criteroia.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 11:16 am 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Righ now code:

Code:
private Type guessType(Object param) throws HibernateException
{
      Class clazz = HibernateProxyHelper.getClass(param);
      return guessType(clazz);
}


throws null pointer exception

After introduction of null type:

Code:
private Type guessType(Object param) throws HibernateException
{
    if(param == null)
     {
           return Hibernate.nullType();
     }
    Class clazz = HibernateProxyHelper.getClass(param);
    return guessType(clazz);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 1:51 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
I agree, why don't you post a JIRA issue and update this thread with its URL so we can voter for it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 7:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Its not really possible to implement this, since JDBC setNull() requires a type code.

OTOH, you could probably use setParameter(null, Hibernate.INTEGER), for example.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 12:53 pm 
Newbie

Joined: Mon Dec 01, 2003 12:41 pm
Posts: 12
Location: Aix en Provence, France
I just tried out, but the null values keep filtering to the request
example :
Query q = session.createQuery("from HbSalle as salle, where salle.nom like :nom and salle.capacite = :capacite");

q.setParameter("nom", null, Hibernate.STRING);
q.setParameter("capacite", null, Hibernate.LONG);

On Mysql query log, it shows :

select hbsalle0_.ID as ID, hbsalle0_.OPTIMIST_LOCK as OPTIMIST2_, hbsalle0_.NOM as NOM, hbsalle0_.LOCALISATION as LOCALISA4_, hbsalle0_.CAPACITE as CAPACITE from SALLE hbsalle0_ where (hbsalle0_.NOM like null )and(hbsalle0_.CAPACITE=null )

_________________
Gauthier P.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ummm, yes, of course....

this is a completely different query to the one in the first post!


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