【问题标题】:What is wrong with this jpql query?(JPA)这个 jpql 查询有什么问题?(JPA)
【发布时间】:2011-08-04 14:40:00
【问题描述】:

您能帮我找出我的应用程序中登录方法的 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 在处理过程中被捕获 调用应用程序 5: UIComponent-ClientId=j_idt13:j_idt17, Message=/WEB-INF/templates/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:一个 创建时发生异常 EntityManager 中的查询:异常 说明:解析语法错误 查询 [SELECT r FROM Role r WHERE r.email=:emailparam, r.password=:passwordparam],第 1 行, 第 46 列:[,] 处的语法错误。 内部异常: MismatchedTokenException(79!=-1)

【问题讨论】:

  • @Facepalmed 嘿伙计,Chilax!我4年前写过这个问题。是的,我当时确实尝试过。你的评论不是很有建设性。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可能忘记添加ANDOR

    喜欢:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 2017-02-25
      • 2011-09-29
      • 2011-10-13
      • 2020-12-12
      相关资源
      最近更新 更多