-->
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.  [ 7 posts ] 
Author Message
 Post subject: using a named query but not require all where params
PostPosted: Thu Jun 22, 2006 8:32 pm 
Newbie

Joined: Fri Mar 17, 2006 3:54 pm
Posts: 6
Hi I was wondering if it is possible to use a named query but not have to specifiy all where params:

example:

SELECT count(*) as n FROM ProcessVO WHERE
completionTime is not null AND
processId=:processId AND
version=:version

I would like for the processId = :processId to not be required and be omitted from the where clause if not specified via a setParameter(). Any way to do this?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 11:14 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can't change the resulting SQL, but you can write the SQL to handle it. Make the requirement be that :processId is set to null if you don't want to search on it, and rewrite the query as
Code:
SELECT count(*) as n FROM ProcessVO WHERE
completionTime is not null AND
(:processId is null or processId=:processId) AND
version=:version

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 1:50 am 
Newbie

Joined: Fri Mar 17, 2006 3:54 pm
Posts: 6
Right that is one option. I guess I was hoping that if I
did not call query.setParameter("processId",....) then the query would still be issued but the processId=:processId part not included. Thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 25, 2006 5:33 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There's nothing like that, as far as I'm aware.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 25, 2006 7:43 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
why not use filters ( assuming you are on hibernate 3 or greater)

http://www.hibernate.org/hib_docs/v3/reference/en/html/filters.html

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 25, 2006 10:17 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
This sort of thing is easier to achieve using the Criteria API. Is there any reason you have to use HQL?

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 4:18 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
damnhandy wrote:
This sort of thing is easier to achieve using the Criteria API. Is there any reason you have to use HQL?

Ryan-


Yes, +1. In fact, that's the thing to learn : use HQL for static queries, optionally parametrized, and use Criteria API for variable queries.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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