-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Passing NULL into SPs through Hibernate named queries.
PostPosted: Mon Sep 05, 2005 5:25 am 
Newbie

Joined: Fri Aug 12, 2005 5:39 am
Posts: 1
Location: Sri Lanka
I have a set of stored procedures in Oracle 10g database which I access using Hibernate named queries. These SPs also accept NULL input parameters as well. But when I try to pass NULL objects I get the following error.

--------------------------------------------------------
Data Access Failure

Hibernate operation: could not execute query; bad SQL grammar [{? = call GET_KOMMUNFOR(?) }]; nested exception is java.sql.SQLException: ORA-06550: line 1, column 13: PLS-00306: wrong number or types of arguments in call to 'GET_KOMMUNFOR' ORA-06550: line 1, column 7: PL/SQL: Statement ignored
« Back
--------------------------------------------------------

BTW I'm using the Sping framework's HibernateTemplate.

Thanks,
Suren.

_________________
Suren Nanayakkara
Sri Lanka


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 4:41 pm 
Newbie

Joined: Wed May 03, 2006 5:09 pm
Posts: 17
I'm having the exact same problem. If I go in and create a separate named query where the parameter is the null literal it works

ex:

ORA-06550

Code:
@NamedNativeQuery(name="test_func",query="{? = call test_func(:testVal) }",resultClass=Test.class,                hints=@QueryHint(name="org.hibernate.callable",value="true"))
...
em.createNamedQuery(test_func).setParameter("testVal",null).getSingleResult();


Works fine

Code:
@NamedNativeQuery(name="test_func_null",query="{? = call test_func(null) }",resultClass=Test.class,                hints=@QueryHint(name="org.hibernate.callable",value="true"))
....
em.createNamedQuery(test_func_null).getSingleResult();



Did you ever discover how to fix it?

Drew


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 16, 2006 6:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
we don't have a .setNull() method on query api because it doesn't really make sense for queries.

It *might* make sense for storedprocedure calls but it just feels so ugly to add this just because you don't have an apropriate method for your sp call ....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 10:17 am 
Newbie

Joined: Wed May 03, 2006 5:09 pm
Posts: 17
Could you elaborate on what 'an apropriate method for your sp call....' might be?

Drew


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 10:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
one that doesn't take null's ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 10:20 am 
Newbie

Joined: Mon Oct 30, 2006 10:14 am
Posts: 1
I was facing the same problem. I tried this solution
use query.setDate(param1, param2) in your DAO layer where param1 is your bind variable and param2 is 'null';

It'll work. and Let me know


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 10:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
that is what he did and claims errors...or?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 10:25 am 
Newbie

Joined: Wed May 03, 2006 5:09 pm
Posts: 17
Unfortunately that method isn't available as we are using Hibernate with EJB3. Thanks for your response though.

Do you know how to do this with the EJB3 Query object?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 11:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
on second thought .setParameter(blah, null); should just work.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 11:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
on third thought, no it won't work since we can't detect at runtime what type you actually is "setting".

you can work around this by casting to hibernat world and use the call that were suggested before.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 12:01 pm 
Newbie

Joined: Wed May 03, 2006 5:09 pm
Posts: 17
Couldn't you just detect what type the field 'should' be and then cast it appropriately or just pass through when the value is null? I'm still confused about why Hibernate does that type checking anyways...it seems like you should be able to set a parameter to be whatever you want and you deal with the consequences.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 12:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh ?

JDBC requires to know the type when you call .setNull too..we can't ignore that.

and we do try and detect it via jdbc getParameterMetaData but if the underlying db doesn't want to tell us then we are out of luck.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 2:45 pm 
Newbie

Joined: Wed Jul 26, 2006 9:53 pm
Posts: 16
Shivali wrote:
I was facing the same problem. I tried this solution
use query.setDate(param1, param2) in your DAO layer where param1 is your bind variable and param2 is 'null';

It'll work. and Let me know


I have tried using all the methods listed below:
query.setParameter(bindName, value, Hibernate.STRING);
query.setString(bindName, value);

where value is null but the query doesnt gives the correct output.

But instead if for null values I do not use and bind parameters and explicitly write my query with the where clause as "... where property IS NULL" then the query result is correct.

The query API says that the setparameter() methods allow null values but they do not seem to be converting it to the the right SQL query. As in a SQL query "...where = null" never works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 2:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you expect a null you query will need to be written to support it.

e.g.


(:param is null || x = :param)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 4:36 pm 
Newbie

Joined: Wed Jul 26, 2006 9:53 pm
Posts: 16
Thank you so much for such a prompt reply. I really appreciate this.

But I couldnt completely get teh solution could you write it more explicitly??


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.