escape特殊字符

qjp7pelc  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(223)

我有以下代码

public User findUser(){

    User user = null;
    String userName = "People's Democratic";

    String queryString = "SELECT user from User user where user.userName = :userName ";

    Map<String, Object> param = new HashMap<String, Object>();  
    param.put("userName", userName);

    try {

        final Query query = entityManager.createQuery(queryString);
        Set<Map.Entry<String, Object>> parameters = param.entrySet();

        for (Map.Entry<String, Object> parameter : parameters) {
            query.setParameter(parameter.getKey(), parameter.getValue());
        }

        user = (User) query.getSingleResult();

    } catch (Exception e) {
        logger.error("Exception in fetching user for the user = "+userName+ " and queryString = "+queryString, e);

        if (e instanceof NoResultException) {
            // User not found
        }
    }
     return user;
}

尽管username=people's democratic的用户出现在用户表中,但此代码在第行中给出了异常
query.getsingleresult()
引发以下异常:
javax.persistence.entitynotfoundexception:找不到id为…..的com.domain.user。。。。。
我试过用
stringescapeutils.escapesql(用户名)blockquote
但没用。
请帮忙。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题