-->
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: HibernateTemplate find(sql, params) exception because params
PostPosted: Sun Sep 20, 2009 1:24 pm 
Newbie

Joined: Sun Sep 20, 2009 1:11 pm
Posts: 1
Hello

I have following problem,
If I call the find method from the hibernatetemplate of spring, I get an error when I
give a String[] with params.
Here some code:
Code:
String[] params = new String[1];
      params[0] = userName;
      List<AppUser> list = this.getHibernateTemplate().find(
            "from AppUser where login = '?'", params);

That works:
Code:
      List<AppUser> list = this.getHibernateTemplate().find(
            "from AppUser where login = '"+username+"'");

If I try it with Arrays of Strings or Objects I get allways that Exception:
Code:
java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
   at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)
   at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:85)
   at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:421)
   at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:393)
   at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:917)
   at at.mt.mydata.dao.impl.UserDAOImpl.findUser(UserDAOImpl.java:21)
   at at.mt.mydata.service.UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:23)
   

login is a String and mapped on that way:
Code:
@Column
    public String getLogin() {
      return login;
   }

   public void setLogin(String login) {
      this.login = login;
   }


Anybody an idea why I get allways that exception?

I tryed also to set the parameter 1-based on second place "params[1]",
but same exception


Top
 Profile  
 
 Post subject: Re: HibernateTemplate find(sql, params) exception because params
PostPosted: Sun Sep 20, 2009 3:18 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi ITCreative,

after I scroogled the exception message Remember that ordinal parameters are 1-based! it turns out that this obviously a misleading one not supposed to surface to the API - I guess.

The hints in different places go something like this:
  • check that you really included any mapping file you require (Hibernate cannot tell until queried if you forgot to provide an entity that is not referenced by another one)
  • check if the ID stuff for the entity in question is alright, meaning esp. data types in class and mapping.

That's I all I have for this issue... Good luck!

CU
Froestel

BTW, this one had been issued as a bug in Spring: http://jira.springframework.org/browse/SPR-1598

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: HibernateTemplate find(sql, params) exception because params
PostPosted: Tue Jun 01, 2010 5:01 am 
Newbie

Joined: Tue Jun 01, 2010 4:58 am
Posts: 1
Hi there,

To resolve your problem,you have to remove ' ' here '?'.
your querry should be :

Object[] params = new Object[]{object1,object2};
getHibernateTemplate().find("select r from Table r where r.field=? and r.field2=?",params);

That's it.

Good luck!


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.