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!