NoResultException找不到查询的实体(NoResultException No entity found for query)

请问我找不到问题在哪里查询它总是让我看到null,即使我手动通过与DB中存储的登录匹配的字符串,当我用“like”更改“=”时,它工作正常,问题与像它返回所有以字符串开头的行我不知道什么是错的,这是我的代码感谢和对我的英语感到抱歉。

public Utilisateur getUserByLogin(String login) { Query req=em.createQuery("select u from Utilisateur u where u.login =:login "); req.setParameter("login", "%"+login+"%"); Utilisateur u = null; try{ u = (Utilisateur)req.getSingleResult(); } catch (NoResultException nre){ } if(u == null){ return null; }else{ return u; } }

please I can't find where is the problem with my query it always show me null even if I pass manually the string that match with login stored in DB, when I change "=" with "like" it works fine, the problem with like it return all rows starting with the string I don't know what's wrong, this is my code thanks and sorry for my english.

public Utilisateur getUserByLogin(String login) { Query req=em.createQuery("select u from Utilisateur u where u.login =:login "); req.setParameter("login", "%"+login+"%"); Utilisateur u = null; try{ u = (Utilisateur)req.getSingleResult(); } catch (NoResultException nre){ } if(u == null){ return null; }else{ return u; } }

最满意答案

尝试

req.setParameter("login", login);

Try with

req.setParameter("login", login);NoResultException找不到查询的实体(NoResultException No entity found for query)

请问我找不到问题在哪里查询它总是让我看到null,即使我手动通过与DB中存储的登录匹配的字符串,当我用“like”更改“=”时,它工作正常,问题与像它返回所有以字符串开头的行我不知道什么是错的,这是我的代码感谢和对我的英语感到抱歉。

public Utilisateur getUserByLogin(String login) { Query req=em.createQuery("select u from Utilisateur u where u.login =:login "); req.setParameter("login", "%"+login+"%"); Utilisateur u = null; try{ u = (Utilisateur)req.getSingleResult(); } catch (NoResultException nre){ } if(u == null){ return null; }else{ return u; } }

please I can't find where is the problem with my query it always show me null even if I pass manually the string that match with login stored in DB, when I change "=" with "like" it works fine, the problem with like it return all rows starting with the string I don't know what's wrong, this is my code thanks and sorry for my english.

public Utilisateur getUserByLogin(String login) { Query req=em.createQuery("select u from Utilisateur u where u.login =:login "); req.setParameter("login", "%"+login+"%"); Utilisateur u = null; try{ u = (Utilisateur)req.getSingleResult(); } catch (NoResultException nre){ } if(u == null){ return null; }else{ return u; } }

最满意答案

尝试

req.setParameter("login", login);

Try with

req.setParameter("login", login);