-->
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.  [ 1 post ] 
Author Message
 Post subject: Named query with possibly-null parameter value
PostPosted: Mon Mar 02, 2009 10:54 am 
Newbie

Joined: Tue Jun 17, 2008 8:34 am
Posts: 7
Hi everyone,

I'm trying to execute a named query with a possible null value. Meaning, one of the parameters MAY be null. I thought the following method would do the trick:

public Query setParameter(String name,
Object val,
Type type)

Bind a value to a named query parameter.

Parameters:
name - the name of the parameter
val - the possibly-null parameter value
type - the Hibernate type

It explicitly refers to a possible null value. I'm hoping I can use queries like 'parameterName := possibleNullValue'. But it doesn't work, at least not this way. What I don't want is to create a different query for each parameter that might be null 'parameterName IS NULL'.

Is there some sort of automation, so hibernate will do the job for me?

Code:
Query query = dbSession.getNamedQuery("XMLMessageType.getByRootElemAndNS");
            query.setString("rootElementName", rootElementName);
            query.setParameter("NS", rootElementNS, Hibernate.STRING);


Hibernate version: 3.2

Mapping documents:
@NamedQuery(name = "XMLMessageType.getByRootElemAndNS", query = "FROM XMLMessageType WHERE rootElementName = :rootElementName AND namespace = :NS", cacheable = true)

Name and version of the database you are using: Oracle 10g

The generated SQL (show_sql=true):

select
xmlmes0_.messageTypeId as essagTy6_5_,
xmlmes0_.guidPath as guidPath5_,
xmlmes0_.namespace as namespace5_,
xmlmes0_.rootElementName as rootElem3_5_,
from
XMLMessageType xmlmes0_
where
xmlmes0_.rootElementName=?
and xmlmes0_.namespace=?

Thanks a lot for your help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.