这个jpql查询出了什么问题?(JPA)

时间:2011-04-13 10:49:32

标签: java sql jpa java-ee-6 jpql

你能帮我找一下我的应用程序中登录方法的JPQL查询中的错误吗?

// Login
public boolean saveUserState(String email, String password) {
    // 1-Send query to database to see if that user exist
    Query query = em
            .createQuery("SELECT r FROM Role r WHERE r.email=:emailparam r.password=:passwordparam");
    query.setParameter("emailparam", email);
    query.setParameter("passwordparam", password);
    // 2-If the query returns the user(Role) object, store it somewhere in
    // the session
    Role role = (Role) query.getSingleResult();
    if (role != null && role.getEmail().equals(email)
            && role.getPassword().equals(password)) {
        FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().put("userRole", role);
        // 3-return true if the user state was saved
        return true;
    }
    // 4-return false otherwise
    return false;
}

执行时出现此错误:

  

严重:JSF1073:   javax.faces.event.AbortProcessingException   在加工期间被捕获   INVOKE_APPLICATION 5:   UIComponent-客户端Id = j_idt13:j_idt17,   消息= / WEB-INF /模板/ BasicTemplate.xhtml   @ 61,63   的ActionListener = “#{securityController.logIn()}”:   javax.ejb.EJBException严重:   /WEB-INF/templates/BasicTemplate.xhtml   @ 61,63   的ActionListener = “#{securityController.logIn()}”:   javax.ejb.EJBException异常   javax.faces.event.AbortProcessingException:   /WEB-INF/templates/BasicTemplate.xhtml   @ 61,63   的ActionListener = “#{securityController.logIn()}”:   javax.ejb.EJBException异常   ..............................引起的   通过:   java.lang.IllegalArgumentException:An   创建时发生异常   EntityManager中的查询:异常   描述:语法错误解析   query [SELECT r FROM Role r WHERE   r.email =:emailparam,   r.password =:passwordparam],第1行,   第46列:[,]处的语法错误。   内部异常:   MismatchedTokenException异常(!79 = - 1)

2 个答案:

答案 0 :(得分:5)

您可能忘记添加ANDOR

像:

Query query = em
            .createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");

答案 1 :(得分:2)

在您的查询中,缺少WHERE子句之间的链接。在两个子句之间添加ANDOR

SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam