i have used the appfuse wrapper method "findByNamedQuery" because it use
findByNamedQueryAndNamedParam and it works perfectly:
public List<T> findByNamedQuery(
String queryName,
Map<String, Object> queryParams) {
String []params = new String[queryParams.size()];
Object []values = new Object[queryParams.size()];
int index = 0;
Iterator<String> i = queryParams.keySet().iterator();
while (i.hasNext()) {
String key = i.next();
params[index] = key;
values[index++] = queryParams.get(key);
}
return getHibernateTemplate().findByNamedQueryAndNamedParam(
queryName,
params,
values);
}
but i didn´t know if the excepción using *"getHibernateTemplate.*
findByNamedQuery*"* was a bug or i am using bad the api.
thanks kan
On Wed, Aug 27, 2008 at 11:35 AM, Kan Sun <[EMAIL PROTECTED]> wrote:
> Hi Miguel
>
> I just encountered exactly the same problem yesterday. I notice that in
> your code there is a line
>
> *return getHibernateTemplate().findByNamedQuery("myquery",queryParams);*
>
> I think you should alternatively use a counterpart of the findByNamedQuery
> e.g.
>
> *getHibernateTemplate().findByNamedQueryAndNamedParam("myquery",
> queryParamNames, queryParamValues);*
>
> Here queryParamNames and queryParamValues are two arrays containing keys
> and values of your input parameter map. In fact why not just use the wrapper
> method provided by AppFuse that takes the Map of input parameters directly?
>
> Kan
>
> 2008/8/27 Miguel Romero <[EMAIL PROTECTED]>
>
> hi appfusers,
>>
>> in appfuse 2.x, i have a problem executing, the follow sentence java:
>> [code]
>> return getHibernateTemplate().findByNamedQuery("myquery",queryParams);
>> [/code]
>> throws the next exception:
>> java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are
>> 1-based!
>>
>> if i change the code to:
>> [code]
>> return findByNamedQuery("myquery",queryParams);
>> [/code]
>> GenericDaoHibernate public method:
>> public List<T> findByNamedQuery(
>> String queryName,
>> Map<String, Object> queryParams) {
>> String []params = new String[queryParams.size()];
>> Object []values = new Object[queryParams.size()];
>> int index = 0;
>> Iterator<String> i = queryParams.keySet().iterator();
>> while (i.hasNext()) {
>> String key = i.next();
>> params[index] = key;
>> values[index++] = queryParams.get(key);
>> }
>> the excepcion isn´t threw. All works well!
>>
>>
>> Is this one a bug in getHibernateTemplate ? i think that the problem is
>> the next definition
>>
>> [code]values[index++] = queryParams.get(key);[/(code]
>>
>> because in getHibernateTemplate the array definition doesn´t started in
>> 1... started in 0
>>
>> [code of HiberanteTemplate]
>> public List findByNamedQuery(final String queryName, final Object[]
>> values) throws DataAccessException {
>> return (List) executeWithNativeSession(new HibernateCallback() {
>> public Object doInHibernate(Session session) throws
>> HibernateException {
>> Query queryObject = session.getNamedQuery(queryName);
>> prepareQuery(queryObject);
>> if (values != null) {
>> for (int i = 0; i < values.length; i++) {
>> queryObject.setParameter(i, values[i]);
>> }
>> }
>> return queryObject.list();
>> }
>> });
>> }
>> [/code of HiberanteTemplate]
>>
>>
>> thanks
>>
>
>