Search code examples
javasqlhibernatejpql

Syntax error in HQL Query "unexpected token"


I am using a select query in hql. But i cannot use in my API.

Getting error as :

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:

Can Someone tell me wat is the error in My HQL

Code for your reference:

Session session = SessionUtil.getSession();

Query query = session.createQuery("SELECT a.mobile, a.email, p.patientId FROM (SELECT l "
        + "from login l where email= :email and password= :password) a INNER JOIN patientprofile p ON a.loginId= p.loginId");
query.setParameter("email", email);
query.setParameter("password", password);
List<Login> logins = query.list();
session.close();
return logins;

Solution

  • I would like to use a native query instead, because both HQL and JPQL accept Subqueries just in SELECT, WHERE or HAVING clause so you can use :

    Query query = session.createNativeQuery("SELECT a.mobile, a.email, p.patientId FROM "
            + "(SELECT * from login l where email= :email and password= :password) a "
            + "INNER JOIN patientprofile p ON a.loginId= p.loginId");
    query.setParameter("email", email);
    query.setParameter("password", password);
    

    read more about this in JPQL documentation

    Subqueries may be used in the WHERE or HAVING clause.