-->
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.  [ 6 posts ] 
Author Message
 Post subject: Optional parameters to named query
PostPosted: Tue May 24, 2005 6:31 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Hi,

Can a named named query be defined, but with optional parameters; i.e. if the value is specified, then include the parameter in the WHERE clause, and just don't include it in the other case (where there is no value for the parameter)?

I've heard about dynamic filters, but those are applied to the way collections of objects are retrieved, and don't know how could they be used to achieve my issue.

Regards,
Enrique Medina.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:45 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Filters are going to be applied every time you issue a query associated to an entity on which you have defined and filter, when the filter is up.

What you can do is: Setup multiple filter for you entity, and only enable the filter when you have a value for it.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:58 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
IMHO, that is not efficient... Think about more than 10 criterion...

I would expect something similar to what iBatis offers:

<statement id="dynamicGetAccountList"
resultMap="account-result" >
select * from ACCOUNT
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="firstName">
(ACC_FIRST_NAME = #firstName#
<isNotNull prepend="OR" property="lastName">
ACC_LAST_NAME = #lastName#
</isNotNull>
)
</isNotNull>
<isNotNull prepend="AND" property="emailAddress">
ACC_EMAIL like #emailAddress#
</isNotNull>
<isGreaterThan prepend="AND" property="id" compareValue="0">
ACC_ID = #id#
</isGreaterThan>
</dynamic>
order by ACC_LAST_NAME
</statement>

See what I mean?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 9:22 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Query by example will do this implicitly. Simply pass an object with the fields you want to include in the search populated, and the others null.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 2:26 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
But, if I set parameters with NULL values, I would get:

SELECT * FROM Users WHERE Name LIKE '%rique%' AND startDate = NULL AND lastAccessed = NULL ...

That select will definitively not work!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 12:21 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Quote:
IMHO, that is not efficient... Think about more than 10 criterion...


ibatis will go through all your defined parameters and set up a where clause, where with hibernate, you would go though you parameters and set up the filters.

I am not sure why you say one is more efficient to the other. Looks pretty much the same to me.

Also, efficiency is relative. You are talking about executing 5-10 java method calls prior to an IO operation. In my opinion, executing a dozen java method calls will not compare to an IO database call over a JDBCDriver over a network;

But that's just my opinion.

Good luck :)

_________________
Vincent Giguère
J2EE Developer


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