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)
Related
Cannot work out why these two statements return a different number of results. They should be the same. The SQL version (correctly) returns 2 results and the HQL version returns (incorrectly) 3 results.
The output of the SQL returns 3 results of tb2.user with values 1, null and null. The where clause means this filters down to 2 results, removing the result with a tb2.user value of 1. However, the HQL version returns 3 results. I would like the HQL to return 2 results.
My SQL
SELECT * FROM table1 as tb1 LEFT JOIN table2 as tb2 ON tb1.user = tb2.blocked WHERE tb2.user <> 1 OR tb2.user is null;
My HQL
SELECT r FROM table1 tb1 LEFT JOIN table2 tb2 ON tb1.user.id = tb2.user.id WHERE tb2.user.id <> :userId OR tb2.user.id is null GROUP BY tb1
Any help on this is much appreciated!
You should not use left joined table's column in where condition .. this work as an inner join
you should move these condition in the related ON clause
and in the second query ( My HQL) you have an improper group by without aggregation function (so the query are not equivalent)
(when you need distinct result .. use Distinct clause )
SELECT r
FROM table1 tb1
LEFT JOIN table2 tb2 ON tb1.user.id = tb2.user.id
AND ( tb2.user.id <> :userId OR tb2.user.id is null )
I have the following query which runs perfectly in MySQL but it gives an error when I written in repository How can I write inner join query in JPARepository?
#Query("Select address from Address a inner join Order o ON a.id=o.pickup_address_id where o.customer_id=: customerId AND a.address LIKE 'C%'")
Set<Address> findPickupAddress(#Param("customerId") Long customerId);
Error : unexpected token: Order near line 1, column 66
order is a reserved word. If you can't rename the table you should use it like:
#Query("Select address from Address a inner join `Order` o ON ...")
There is no ON in JPQL so the query is updated as :
#Query("Select a from Address a , Order o where a.id=o.pickupAddress AND o.customer.id=:customerId AND a.address LIKE 'C%'")
Address findPickupAddress(#Param("customerId") Long customerId);
}
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")
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.
I keep trying variations of this query and can't seem to make this happen. I've also referenced this post: Path Expected for Join! Nhibernate Error and can't seem to apply the same logic to my query. My User object has a UserGroup collection.
I understand that the query needs to reference entities within the object, but from what I'm seeing I am...
#NamedQuery(
name = "User.findByGroupId",
query =
"SELECT u FROM UserGroup ug " +
"INNER JOIN User u WHERE ug.group_id = :groupId ORDER BY u.lastname"
)
select u from UserGroup ug inner join ug.user u
where ug.group_id = :groupId
order by u.lastname
As a named query:
#NamedQuery(
name = "User.findByGroupId",
query =
"SELECT u FROM UserGroup ug " +
"INNER JOIN ug.user u WHERE ug.group_id = :groupId ORDER BY u.lastname"
)
Use paths in the HQL statement, from one entity to the other. See the Hibernate documentation on HQL and joins for details.
You need to name the entity that holds the association to User. For example,
... INNER JOIN ug.user u ...
That's the "path" the error message is complaining about -- path from UserGroup to User entity.
Hibernate relies on declarative JOINs, for which the join condition is declared in the mapping metadata. This is why it is impossible to construct the native SQL query without having the path.
You'll be better off using where clauses. Hibernate does not accept inner joins for tables where the PK/FK relationship is not there between Entities
do
SELECT s.first_name, s.surname, sd.telephone_number FROM Student s, StudentDetails sd WHERE s.id = sd.student_id
instead of
SELECT s.first_name, s.surname, sd.telephone_number FROM Student s INNER JOIN StudentDetails sd on s.id = sd.student_id
The latter will only work if Student's id (s.id) is referenced as FK on StudentDetails (sd.student_id)) table design / erd