我有以下代码
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;
}
虽然用户表中存在用户名= 人民民主党的用户,但此代码在线提供例外
query.getSingleResult()
抛出以下异常:
javax.persistence.EntityNotFoundException:无法找到 com.domain.User,id为.....
我尝试使用
转义用户名StringEscapeUtils.escapeSql(用户名) 块引用
但它没有帮助。
请帮忙。提前谢谢。