2

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;
2
  • 1
    you can share the full error @mree please Commented Sep 12, 2017 at 10:29
  • org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 44 [SELECT a.mobile, a.email, p.patientId FROM (SELECT l.LoginId, l.email, l.mobile from com.innovellent.hibernate.restapi.model.Login l where email= :email and password= :password) a INNER JOIN com.innovellent.hibernate.restapi.model.PatientProfile p ON a.loginId= p.loginId] Commented Sep 12, 2017 at 10:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Note that HQL subqueries can occur only in the select or where clauses.

So you can use nativeQuery but I would recommend to use HQL whenever possible to avoid database portability hassles, and to take advantage of Hibernate's SQL generation and caching strategies. instead of native query you can use join method and link them.

Notice HQL works with persistent objects and their properties.HQL queries are translated by Hibernate into conventional SQL queries which in turns perform action on database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.