QuerySyntaxException: unexpected token: - java

In MySQL it works:
SELECT * FROM carparks a
LEFT JOIN (SELECT * FROM locales_carparks)
c ON a.carpark_id=c.carpark_id
Hot to translate it to JPA:
#Query("SELECT a FROM Carparks a LEFT JOIN("
+"SELECT b FROM a.locales b"
+")")
IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:
( near line 1, column 72 [SELECT a FROM
database.model.carpark.Carparks a LEFT JOIN(SELECT b
FROM a.locales b)]
I've simplified example to show the essence of the problem. Normally I use justSELECT a FROM Carparks a LEFT JOIN a.locales and it works, but in my case I want to use nested SELECT because my query is much more complex

You could use a simple alternative
create view v_carparks as
SELECT * FROM carparks a
LEFT JOIN (SELECT * FROM locales_carparks)
c ON a.carpark_id=c.carpark_id
And use it for the query
#Query("SELECT a FROM v_carparks")
Especially if the query is complicated, this would be cleaner to have a huge query in a view to hide that complexity.
EDIT :
You can't used a nested query for join. This is written in the HQL documentation like this :
Note that HQL subqueries can occur only in the select or where clauses.
This could be explain for the mapping system. Hard to do the mapping with a subqueries result.

You can write it like this
#Query("SELECT a FROM Carparks a LEFT JOIN Locales b on a.carpark_id = b.carpark_id")

Related

join and where clause in hibernate

I am using join and where clause in hibernate 3.but i cant reach the solution.I got the error.
Query qry= session.createQuery("SELECT addemployee.eid,addemployee.fname,addemployee.location,"
+ "empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ='"+id+"')");
List l = qry.list();
Iterator it=l.iterator();
while(it.hasNext())
{
Object rows[] = (Object[])it.next();
System.out.println(rows[0]+separator+rows[1]+separator+rows[2]+separator+rows[3]+separator+rows[4]);
}
Issue: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 1, column 127 [SELECT addemployee.eid,addemployee.fname,addemployee.location,empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ='206')]
Try to use session.createSQLQuery() instead.
and don't put enclosed apostrophe (''), you are inputting numbers, not varchar.
like this.
Query qry= session.createSQLQuery("SELECT addemployee.eid,addemployee.fname,addemployee.location,"
+ "empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ="+id+")");
Hibernate Session's createQuery() method requires valid HQL syntax. You can check how to write joins here.
Basically, in HQL you work with your entities, not SQL tables. So you don't need to write ON, because you already map association between entities.
If you still want to write native SQL query, you need to use
session.createSQLQuery(); instead

Hibernate Criteria: hibernate left join without association

I am a Hibernate newbie and i have this below query. It is working as i expected. These two tables are not associated. Is there a way to get the same result by using Criteria API or how can i run this query via Hibernate ? Any help would be appreciated.
SELECT p.title, c.content
FROM posts p
LEFT JOIN comments c ON p.id = c.post_id
WHERE p.status = 'A' AND (p.title iLIKE '%r%' OR c.content iLIKE '%r%');
Criteria API needs a path between entities, so I'm not sure this join could be done using Criteria API. Better do it with HQL if you have Hibernate >= 5.1:
select p.title, c.content
from org.example.Posts p
left outer join org.example.Comments c
on p.id = c.id
where p.status = 'A' AND (lower(p.title) LIKE '%r%' OR lower(c.content) LIKE '%r%');
Still, you could stick to using SQL queries with Hibernate or better still, create association between Posts and Comments.

How to write a join query in HQL

I have 3 tables in DB.
I want to make join query like this:
from Installment i
join Payment p on i.vcode=p.Installment_Vcode and p.vcode=:vcode
but when I run it this error happens:
unexpected token: on near line 1, column 47 [from information.Installment i join Payment p on i.vcode=p.Installment_Vcode]
HQL doesn't have an on operator. Joins can only be done on associations between entities, and can have an optional with clause.

HQL Query with alias

I am trying to execute an hql query with aliases
select **clbs.id as id**
from ClaimDO cl, ClaimBillSummaryDO clbs, HospitalDO h
where clbs.parentGuidObj.id=cl.id and h.id=cl.hospitalSeq and cl.id= '10721'
and I get the following error
org.hibernate.QueryException: , expected in SELECT
However the query runs without error if i remove the alias
select **clbs.id**
from ClaimDO cl, ClaimBillSummaryDO clbs, HospitalDO h
where clbs.parentGuidObj.id=cl.id and h.id=cl.hospitalSeq and cl.id= '10721'
Why are you not using mappings to join your entities? You might as well use native queries to do this. HQL would look more like the following. I have omitted the HospitalDO join since it doesn't look meaningful.
select clbs.id from ClaimDO cl join cl.parentGuidObj clbs where cl.id = :id

How use hibernate criteria for a left outer join without direct link into the two tables

I'm trying to translate a SQL quest into Hibernate criteria.
My quest is working in SQL :
select * from objective
left outer join conditionstate
on objective.conditionid = conditionstate.conditionid
and conditionstate.personid = XXXX
where objective.toto_id = YYYYY
The objective is not directly mapped to the condition_state, but into a condition.
objective --> condition <-- condition_state
I've tested something like :
final DetachedCriteria criteriaObjective =
DetachedCriteria.forClass(Objective.class);
criteriaObjective.createAlias("conditionState", "conditionState", Criteria.LEFT_JOIN);
without success..
It's hard to suggest something without seeing your actual mappings. Going by your explanation and assuming that both condition and conditionState are mapped as many-to-one, you'd write something like:
final DetachedCriteria criteriaObjective =
DetachedCriteria.forClass(Objective.class);
criteriaObjective
.createCriteria("condition", Criteria.LEFT_JOIN)
.createCriteria("conditionState", Criteria.LEFT_JOIN)
.add(Restrictions.eq("personid", "XXXX") );
criteriaObjective.add(Restrictions.eqProperty("toto_id", "YYYY") );
Note that the above is NOT equivalent to the SQL query you've provided because "personid" condition will be generated as part of "WHERE" clause. As far as I know it's impossible to do a left join with condition using Criteria API - you may need to use HQL instead which provides with keyword for that exact purpose:
select o from objective as o
left join o.condition as c
left join c.conditionState as cs
with cs.person_id = 'XXXX'
and o.toto_id = 'YYYY'

Categories

Resources