-->
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.  [ 3 posts ] 
Author Message
 Post subject: Optional Parameters in a Hibernate Query
PostPosted: Wed Oct 13, 2004 5:47 pm 
Newbie

Joined: Wed Oct 13, 2004 5:39 pm
Posts: 1
I have a named query in my hbm file. I would like to have some of the parameters optional so that if they aren't provided in the parameter map/ array of parameter objects then it is not included inthe where clause.

I have the query below. I would like to be able to leave off Area so the where clause becomes:
(fooTable.fooTablePK.Facility = :Facility
AND fooTable.fooTablePK.Unit = :Unit
AND fooTable.OpSystem = :OpSystem
AND fooTable.Division = :Division
AND fooTable.SystemCode = :SystemCode
AND fooTable.PClass = :PClass)

What is the best way to do this in Hibernate?

Thanks,
brian_a_lee@hotmail.com

Hibernate version:
2
Mapping documents:
SELECT fooTable
FROM com.nothingcorp.server.data.dao.FooTable fooTable
WHERE
(fooTable.fooTablePK.Facility = :Facility
AND fooTable.fooTablePK.Unit = :Unit
AND fooTable.OpSystem = :OpSystem
AND fooTable.Division = :Division
AND fooTable.Area = :Area
AND fooTable.SystemCode = :SystemCode
AND fooTable.PClass = :PClass)
ORDER BY
fooTable.fooTablePK.Facility,
fooTable.fooTablePK.Unit,
fooTable.fooTablePK.ShutdownNbr,
fooTable.fooTablePK.ShutdownDateFrom,
fooTable.fooTablePK.ShutdownDateTo
Code between sessionFactory.openSession() and session.close():
Query q = s.getNamedQuery(name);
if (null != params) {
for (Iterator i=params.entrySet().iterator(); i.hasNext(); ) {
Map.Entry entry = (Map.Entry) i.next();
setParameterValue(q, (String) entry.getKey(), entry.getValue());
}
}
return q.list()
Name and version of the database you are using:
Oracle 9.2.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 13, 2004 10:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You can do this in Hibernate3 using the dynamic filters. This is not acheivable in Hibernate2.


Top
 Profile  
 
 Post subject: But how?
PostPosted: Tue May 24, 2005 2:59 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Sorry, but how can "optional parameters" be achieved using dynamic filters in Hibernate3?


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