开发者

message: Remember that ordinal parameters are 1-based! (and I am)

开发者 https://www.devze.com 2023-04-05 20:09 出处:网络
I\'m trying to make a more generic method for a login. public boolean login(String login, String pass)

I'm trying to make a more generic method for a login.

public boolean login(String login, String pass) 
             throws ClassNotFoundException, SQLException {   
        open();   
        q = s.createQuery("select u from Usuario u 
                               where u.login =:id1 and u.pass = :id2");   
        q.setString("id1", login);  
        q.setString("id2", pass);  
        if(q.uniqueResult()!=null)  
        return true;  
        else  
            return false;
}

My code was working just fine but now I wanted to do something like:

 public boolean login(String login, String pass) 
          throws ClassNotFoundException, SQLException {  

        return paramFunction("select u from Usuario u 
             where u.login = ?1 and u.pass=?2", login, pass);
 }
 public boolean paramFunction(String query, Object... params){
    try {
        open();
    } catch (ClassNotFoundException e) {
        e.printStackTrace开发者_高级运维();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    q = s.createQuery(query);
            for (int i = 1; i <= params.length; i++) {
                q.setParameter(i, params[i-1]);}
    if (q.uniqueResult() != null)
        return true;
    else
        return false;

but now I'm getting:

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!

although you can see I'm using a 1-based approach. What's wrong with what I'm doing?

Thanks for the help!


Actually, in Hibernate parameters are 0 based. See docs.

position - the position of the parameter in the query string, numbered from 0.


This error did also occur when i was calling the HibernateTemplate with a non-matching count of parameters.

String query = "select * from table where x=? and y=? and z=? order by timestamp desc";
HibernateTemplate ht = getHibernateTemplate();
ht.find(query, parm1, parm2, parm3, parm4); 

// i was calling find() with 4 parmeters while the query only expects 3 parameters -> IndexOutOfBoundsException
0

精彩评论

暂无评论...
验证码 换一张
取 消