简体   繁体   English

在尝试使用WHERE子句时获取异常

[英]Getting exception as I try use the WHERE clause

While trying to execute the query with where clause, I get an exception : 尝试使用where子句执行查询时where出现异常:

SEVERE: Unknown column 'suhail03' in 'where clause'
org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.loader.Loader.doList(Loader.java:2223)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
            ...

Here suhail03 is the condition in the where clause. suhail03where子句中的条件。 Where am I going wrong ? 我要去哪里错了?

 private boolean appAuthorized(String username) {

    boolean hasAuthorized = false;
    try {
        Session session = new HVR().getSession();

        String hql = "from UCred where username =" + username; // WHERE QUERY

        List list = session.createQuery(hql).list();
        Iterator iterator = list.iterator();
        while(iterator.hasNext()) {
            UCred user = (UCred)iterator.next();
            if(user.getAccessToken().compareTo("null") == 0) {
                hasAuthorized = false;
            }else {
                hasAuthorized = true;
            }
        }
    }catch(Exception exc) {
        exc.printStackTrace();
        return false;
    }
    return hasAuthorized;

}

Without parameter binding, you have to concatenate the parameter String like this (bad code) : 没有参数绑定,您必须像这样(错误代码)连接参数String:

String hql = "from UCred where username =" + "'" + username + "'";

it's better to use positional parameters 最好使用位置参数

String hql = "from UCred ucred where ucred.username = :username";

List list = session.createQuery(hql).
            .setParameter("username", username)
             .list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM