简体   繁体   中英

What is wrong with this jpql query?(JPA)

Can you help me find the error in my JPQL query of the login method in my application please?

// 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;
}

I am getting this error when it executes:

SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt13:j_idt17, Message=/WEB-INF/templates/BasicTemplate.xhtml @61,63 actionListener="#{securityController.logIn()}": javax.ejb.EJBException SEVERE: /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 .............................. Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing the query [SELECT r FROM Role r WHERE r.email=:emailparam, r.password=:passwordparam], line 1, column 46: syntax error at [,]. Internal Exception: MismatchedTokenException(79!=-1)

You probably forgot to add AND or OR

like:

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

In your query the link between the WHERE clauses is missing. Add an AND or an OR between both clauses:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM