unexpected end of subtree JPQL - java

I have a query which count a given code from 4 tables. I tested this query first in postgresql, it worked as I expected so I tried to translate it to JPQL and I got this error :
java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of
subtree [ select ((select count(*) from *.Record r where
r.codeCampaign =?1 ) + (select count(*) from *.AccountAssociationAction aaa where aaa.codeCampaign = ?1) +
(select count(*) from *.CampaignPayment cp where cp.pk.campaignCode = ?1) + (select count(*) from
*.ActionPeriodDepartment apd
where apd.id.codeCampaign = ?1))]
I can't figure out what is wrong and what does Hibernate mean by "unexpected end of subtree"
The postgresql query :
select (select count(*) from account_association_action aaa where
aaa.code_campaign = 'CAMP01') + (select count(*) from _campaign_payment cp
where cp.campaign_code = 'CAMP01') + (select count(*) from record r where
r.code_campaign ='CAMP01') + (select count(*) from action_period_department apd
where apd.code_campaign = 'CAMP01');
the JPQL query :
#Query(value=" select (select count(*) from Record r where r.codeCampaign =?1 ) + " +
" (select count(*) from AccountAssociationAction aaa where aaa.codeCampaign = ?1) +" +
" (select count(*) from CampaignPayment cp where cp.pk.campaignCode = ?1) +" +
" (select count(*) from ActionPeriodDepartment apd where apd.id.codeCampaign = ?1)")
int countCampaignCodeUses(String campaignCode);

It looks like you need to add nativeQuery=true to #Query annotation otherwise JPA is failing to make sense of a query without from

Related

SQL code does not work when converted to JPQL code (ORA-00904 error code)

Thank you very much for looking at this post :). I have the following problem: I have a piece of SQL code that runs perfectly on the Oracle database. The problem comes when I try to run as JPQL code. I get the following error in the Eclipse IDE:
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "THIRDTABLE"."NAME": invalid identifier.
The SQL that works is the following:
SELECT e.status_q,
q.name,
q.masterdatastatus_id
FROM firstTable e
JOIN secondTable q ON e.status_q = q.id
AND q.masterdatastatus_id = e.status_s
WHERE q.id != 7122
AND e.qserie_id = 1296
AND q.masterdatastatus_id not in
(SELECT l1.status_s
FROM thirdTable l1
WHERE (l1.id,
l1.name) not in
(SELECT l2.id,
l2.name
FROM thirdTable l2
WHERE l2.status_s = 1))
AND
(SELECT count(*)
FROM thirdTable l3
WHERE l3.status_s = q.masterdatastatus_id) =
(SELECT count(*)
FROM thirdTable l4
WHERE l4.status_s = 1)
AND q.masterdatastatus_id in
(SELECT x.status_s
FROM thirdTable x
LEFT JOIN thirdTable y ON x.id = y.id
AND x.name = y.name
WHERE y.id IS NOT NULL
AND y.status_s = 1 )
ORDER BY q.id DESC;
This is the JPQL code that doesn't work:
public static final String QUERY = "SELECT e.status, q.name, q.masterdatastatus.id FROM firstTableBE e JOIN secondTableBE q "
+ "ON e.status = q.id AND q.masterdatastatus.id = e.statusS WHERE "
+ "q.id != :STATUS_Q AND e.qSerieOid = :QSERIE_ID AND "
+ "q.masterdatastatus.id NOT IN (SELECT l1.status from ThirdTableBE l1 where (l1.oid, l1.name) NOT IN (SELECT l2.oid, l2.name from ThirdTableBE l2 where l2.status = 1)) AND "
+ "(SELECT COUNT(l3) from ThirdTableBE l3 where l3.status = q.masterdatastatus.id) = (SELECT COUNT(l4) from ThirdTableBE l4 where l4.status = 1) AND "
+ "q.masterdatastatus.id IN (SELECT x.status from ThirdTableBE x LEFT JOIN ThirdTableBE y on x.oid = y.oid and x.name = y.name where y.oid is not null and y.status = 1) "
+ "ORDER BY q.id DESC";
I guess the problem comes from this part of the code:
"q.masterdatastatus.id NOT IN (SELECT l1.status from ThirdTableBE l1 where (l1.oid, l1.name) NOT IN (SELECT l2.oid, l2.name from ThirdTableBE l2 where l2.status = 1))"
Are multiple expressions in subqueries not supported in JPQL? Is it the pair after the 'where' statement ? When I try with only one expression (with l1.oid for example), it works but the result is not the expected one. Also if I remove the code above, it works, but again, it's not the result I want.
Thanks in advance for the help! :)
I solved the problem by running the SQL code with the createNativeQuery method from the entityManager. It seems that JPQL doesn't really like pairs in the where statement. Maybe the JPA version I use does not support pairs (I use JPA 2.6.4). This is the code :
Query query = entityManager
.createNativeQuery("select e.status_q, q.name, q.masterdatastatus_id from firstTable e "
+ "join secondTable q on e.status_q = q.id and q.masterdatastatus_id = e.status_s "
+ " WHERE q.id != 7122 and e.qserie_id = 1296 and "
+ "q.masterdatastatus_id not in (select l1.status_s from thirdTable l1 where (l1.id, l1.name) not in (select l2.id, l2.name from thirdTable l2 where l2.status_s = 1)) and "
+ "(select count(*) from thirdTable l3 where l3.status_s = q.masterdatastatus_id) = (select count(*) from thirdTable l4 where l4.status_s = 1) and "
+ "q.masterdatastatus_id in (select x.status_s from thirdTable x left join thirdTable y on x.id = y.id and x.name = y.name where y.id is not null and y.staatus_s = 1 ) "
+ "order by q.id desc");
List<Object> result = query.getResultList();
Thanks again Abenamor for the solution :).

Mysql CASE statement with subquery in JPQL

In MySQL
SELECT SUM(CASE WHEN (t.code = 'ITEM') THEN (SELECT i.amount FROM Billable_Item i WHERE i.Billable_Item_id = ri.billable_Id) ELSE 0 END) FROM Reservation_Item ri JOIN RESERVATIoN r on r.RESERVATION_ID = ri.RESERVATION_ID JOIN billable_Type t on t.billable_Type_id = ri.billable_Type_id WHERE r.reservation_id = xxx
In Hibernate JPQL
#Query(
"SELECT SUM(CASE WHEN (t.code = 'ITEM') THEN (SELECT i.amount FROM BillableItem i WHERE i.id = ri.billableId) ELSE 0 END) " +
"FROM ReservationItem ri JOIN ri.reservation r JOIN ri.billableType t WHERE r.id = :reservationId")
above SQL statement work well in MySQL, however, when converted to Hibernate JPQL it hit an exception
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: query [SELECT SUM(CASE WHEN (t.code = 'ITEM') THEN (SELECT i.amount FROM com.flyt.domain.BillableItem i WHERE i.id = ri.billableId) ELSE 0 END) FROM com.flyt.domain.ReservationItem ri JOIN ri.reservation r JOIN ri.billableType t WHERE r.id = :reservationId]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:257)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:104)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:79)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:222)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:200)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1739)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)

Hibernate or HQL Select on the result of another SELECT

Is there a way using HQL on the following query?
SELECT userId, pwd, pwdDate FROM
(SELECT userId, AES_DECRYPT(pwd, 'key_str') as pwd, pwdDate
FROM UserHistory order by pwdDate desc limit 5 ) AS A
WHERE pwd = :pwd
The following worked.
SELECT *
FROM UserHistory order by pwdDate desc limit 5
The above sql can be the following in hibernate.
Criteria criteria = session.createCriteria(UserHistory.class);
criteria.addOrder(Order.desc("pwdDate"));
List<UserHistory> list = criteria.setMaxResults(5).list();
The following worked. The key is to createSQLQuery for Native SQL.
String SQL =
"SELECT A.* FROM \n" +
"(select * \n" +
" from user_history$ order by pwdDate desc limit 5 ) AS A \n" +
"where pwd = AES_ENCRYPT(:pwd, 'key_str') \n";
Query query = session.createSQLQuery(SQL);
query.setParameter("pwd", psw);
List<UserHistory> list = query.list();

Is it hibernate bug?

I write
String sql = "select candidate_skill.candidate_id from candidate_skill " +
"inner join skill on skill.id = candidate_skill.skill_id " +
"where skill_id in (:skillIdList) group by candidate_skill.candidate_id " +
"Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in (:skillIdList) )";
sql = sql.replace(":skillIdList", generateSkillIdList(skills));
Query query = session.createSQLQuery(sql);
List<Candidate> candidates = query.list();
It works good
second situation:
String sql = "select candidate_skill.candidate_id from candidate_skill " +
"inner join skill on skill.id = candidate_skill.skill_id " +
"where skill_id in :skillIdList group by candidate_skill.candidate_id " +
"Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in :skillIdList )";
Query query = session.createSQLQuery(sql).setParameterList("skillIdList", skills);
List<Candidate> candidates = query.list()
log:
Hibernate: select candidate_skill.candidate_id from candidate_skill inner join skill on skill.id = candidate_skill.skill_id where skill_id in (?, ?) group by candidate_skill.candidate_id Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in ?, ? )
it doesn't works
and third:
String sql = "select candidate_skill.candidate_id from candidate_skill " +
"inner join skill on skill.id = candidate_skill.skill_id " +
"where skill_id in :skillIdList group by candidate_skill.candidate_id " +
"Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in (:skillIdList) )";
Query query = session.createSQLQuery(sql).setParameterList("skillIdList", skills);
List<Candidate> candidates = query.list();
log:
Hibernate: select candidate_skill.candidate_id from candidate_skill inner join skill on skill.id = candidate_skill.skill_id where skill_id in (?, ?) group by candidate_skill.candidate_id Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in (?, ?) )
it works good
P.S. Pay attention to the Brackets around :skillIdList
if I use setParameterList("argument",value) and argument in query 2 times, then first time hibernate substitutes brackets and in second - none
The syntax of IN requires the brackets.
As of why the 3rd example is working, two guesses:
hibernate has a functionality to automatically append missing brackets, but somehow this doesn't extend to subqueries
the sql server executes the sub-select first, and then the other query becomes redundant with the parameters you've passed, and so is not executed.

why native sql query can't run in hibernate and got exception

When I run the following SQL query in Hibernate I got org.hibernate.exception.GenericJDBCException: could not execute query:
select cast(DECRYPTBYKEY(cc.EncryptCardno) as varchar(30)) as cardNo
from stmtOfAccounts s, creditCards cc
where s.creditCardId = cc.id and s.id = :stmtOfAccountId
This is the HQL:
String hqlQuery = String.format(
"select cast(DECRYPTBYKEY(cc.EncryptCardno) as varchar(30)) as cardNo " +
"from stmtOfAccounts s, creditCards cc " +
"where s.creditCardId = cc.id and s.id = :stmtOfAccountId");

Categories

Resources