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
Related
I'm running the following query using a jdbc template against a MySQL db:
SELECT d.*, a.mongo_id as yyyyy_profile_mongo_id,t.mongo_id as targeting_profile_mongo_id, CAST(xxxxx_type AS SIGNED) AS xxxxx_type_int
LEFT JOIN zzzzz t on t.id = d.targeting_profile_id
LEFT JOIN yyyyy a on a.id = d.yyyyy_profile_id
FROM xxxxx d
When attempting to get a string from a ResultSet using the aliased name "targeting_profile_mongo_id" I get a "Invalid Column Name" error
I can see in the debugger that even though I did "as targeting_profile_mongo_id" the columnName still shows as the original value of "mongo_id" and only the column label is changed.
Are there any work arounds here without having to loop through the metadata to find the column index?
I found a solution after some more digging! Looks like you can somehow get around this by putting a concat around the columns causing the issue.
like this
SELECT d.*, CONCAT(a.mongo_id,'') as yyyyy_profile_mongo_id,CONCAT(t.mongo_id,'') as targeting_profile_mongo_id, CAST(xxxxx_type AS SIGNED) AS xxxxx_type_int
LEFT JOIN zzzzz t on t.id = d.targeting_profile_id
LEFT JOIN yyyyy a on a.id = d.yyyyy_profile_id
FROM xxxxx d
mysql column alias not working, have to create an empty concatenation to make it work
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 the following query, which is working when I use it directly at the db:
#NamedQuery(name = "Sentitems.findWhereSendingDateTimeIsYesterdayByStatus",
query = "SELECT s FROM Sentitems s WHERE s.status = :status AND DATE_FORMAT(s.sendingDateTime, '%Y-%m-%d') = SUBDATE(CURDATE(),1)")
When running the application, a NoViableAltException is thrown:
Exception Description: Syntax error parsing the query [Sentitems.findWhereSendingDateTimeIsYesterdayByStatus: SELECT s FROM Sentitems s WHERE s.status = :status AND DATE_FORMAT(s.sendingDateTime, '%Y-%m-%d') = SUBDATE(CURDATE(),1)], line 1, column 66: unexpected token [(].
Internal Exception: NoViableAltException(83#[()* loopback of 383:9: (d= DOT right= attribute )*])
Try with #NamedNativeQuery. You seem to be using some DB specific syntax.
Like Balaji Krishnan said in the comments, the solution is to use #NamedNativeQuery instead of #NamedQuery.
I am new to querydsl and already in love with it, but I can't get how to use an alias in this query.
QProduct product = QProduct.product;
JPQLQuery jPQLQuery =
from(product)
.where(product.name.locate(searchTerm).as("score").gt(0).or(product.code.locate(searchTerm).as("score").gt(0))).groupBy(product.baseProd);
List<Product> matchedNames = jPQLQuery.orderBy(new NumberPath<Integer>(Integer.class, "score").asc()).offset((pageNumber-1)*10).limit(10).list(product);
my first thought was something like this which throws an error with the generated query as:
java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:
as near line 3, column 31 [select product from
mesoft.meshopframework.model.Product product where
locate(?1,product.name) as score > ?2 or locate(?1,product.code) as
score > ?2 group by product.baseProd order by score asc]
could someone show me the coorect way to do this?? I hope it's not necessary for DB to calculate product.name.locate(searchTerm) or the other one again for sorting,
thanks
JPQL has a more restricted syntax compared to SQL, but I don't think your example would work in SQL either.
You can't use aliases in the where part and for order you will need pick one of the locate expressions or order by both of them.
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.