How to write a join query in HQL - java

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.

Related

JOIN with multiple OR in HQL

Below is a SQL query with multiple joins and conditions which gives the desired output, I want to convert the below query to HQL
select * from customer c
join customer_geo_rel cg on c.id=cg.customer_id
join geography g on cg.geo_id=g.id
join geo_geo_hierarchy gghCluster on g.id = gghCluster.geo_id
join geo_geo_hierarchy gghDivision on gghCluster.geo_id = gghDivision.parent_geo_id
join role_data_rel rdr on gghCluster.geo_id = rdr.permission_data_id OR
gghDivision.parent_geo_id = rdr.permission_data_id OR
g.id=rdr.permission_data_id
What's the problem? HQL supports the ON clause for joins and if you use a very old version of Hibernate, you can use the WITH clause like e.g. .. join g.roleDataRelList rds on ...

Getting error in executequery in hibernate

I have written a hql query in grails inside controller and while executing it i am getting error such as
unexpected token: where near line 1, column 166 [FROM com.ashwin.Training tr where tr.id NOT IN (SELECT t.training_id from com.ashwin.User u INNER JOIN com.ashwin.TrainUser t on u.id=t.user_id where u.id=t.user_id where u.id=:uid)]".
MY Hibernate query is
def currentUser = springSecurityService.currentUser.id
def trainingList=Training.executeQuery("FROM Training tr where tr.id NOT IN (SELECT t.training_id from User u INNER JOIN TrainUser t on u.id=t.user_id where u.id=t.user_id where u.id=:uid)",[uid:currentUser])
[trainingLists:trainingList]
In inner select you have this
t on u.id=t.user_id where u.id=t.user_id where u.id=:uid
Double where is incorrect. And also you already define join rule. So the correct statement is
FROM Training tr where tr.id NOT IN
(SELECT t.training_id from User u INNER JOIN TrainUser t
on u.id=t.user_id where u.id=:uid)

QuerySyntaxException: unexpected token:

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")

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 expecting "all", found 'JOIN'

I'm getting
QuerySyntaxException: expecting "all", found 'JOIN' near line 1, column 50 [SELECT p FROM com.rr.model.Person p FETCH JOIN p.positions pos WHERE p.id=:id]
exception with Hibernate. Changing 'JOIN' to 'ALL' does not help and produce following error:
QuerySyntaxException: expecting "properties", found 'p' near line 1, column 54 [SELECT p FROM com.rr.model.Person p FETCH ALL p.positions pos WHERE p.id=:id]
What's going on and how the correct query should look like?
The problem is I switched syntax to 'FETCH JOIN' form the correct one: 'JOIN FETCH'. The correct query should look like this
SELECT p FROM Person p JOIN FETCH p.positions pos WHERE p.id=:id
The correct syntax with 'ALL' looks like this:
SELECT p FROM Person p FETCH ALL PROPERTIES WHERE p.id=:id

Categories

Resources