I'm using implementing the interface that extends from org.springframework.data.repository.CrudRepository and org.springframework.data.jpa.repository.JpaSpecificationExecutor
The problem I hava a query org.springframework.data.jpa.repository.Query with param org.springframework.data.repository.query.Param
I have this Query..
#Query(value = " SELECT c FROM Ctype c WHERE c.code = :code"
+ " FOR UPDATE nowait ")
Ctype findOneByCodeNoWait(
#Param("code") String code);
I have this error:
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.company.persistence.entity.Ctype org.company.persist.repository.CtypeRepository.findOneByCodeNoWait(java.lang.String)!
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FOR near line 1, column 111 [ SELECT c FROM org.company.persistence.entity.Ctype c WHERE c.code = :code FOR UPDATE nowait ]
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FOR near line 1, column 111 [ SELECT c FROM org.company.persistence.entity.Ctype c WHERE c.code = :code FOR UPDATE nowait ]\"}}"
I was checking another post, but they uses EntityManager...
How solve this?
Try this way:
#Lock(LockModeType.PESSIMISTIC_READ)
#QueryHints(#QueryHint(name = "javax.persistence.lock.timeout",value = "0"))
#Query(value = " SELECT c FROM Ctype c WHERE c.code = :code")
I have tested this with my ongoing project and this code:
#Lock(LockModeType.PESSIMISTIC_READ)
#QueryHints(#QueryHint(name = "javax.persistence.lock.timeout",value = "0"))
#Query("select t from Event t left join fetch t.details ")
List<Event> findAllWithDetails();
`
generates sql like:
SELECT event0_.event_id ...
FROM EVENTS event0_
left outer join event_details details1_
ON event0_.event_id = details1_.event_id
FOR UPDATE NOWAIT
Related
I'm migrating an application, that uses DB2 database, from Spring(IBM Websphere) to Springboot(Embedded Tomcat).
The existing application, that uses hibernate 4.1.9.Final, runs perfectly using this FETCH query :
#Query(
"SELECT ssss FROM SSSSPackageDiscountLoad ssss, SSSBasicLoad ttt WHERE ssss.schemeId = ttt.id AND ttt.code = :schemeCode AND "
+ "ssss.ruleCode = :ruleCode ORDER BY ssss.effDate DESC FETCH FIRST 1 ROWS ONLY")
The migrated springboot(version 2.1.7.RELEASE) app uses hibernate 5.3.10.Final.
Starting the application gives me this issue :
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: FETCH near line 1, column 255
You could use the equivalent statement:
#Query(
"SELECT ssss FROM SSSSPackageDiscountLoad ssss, SSSBasicLoad ttt WHERE ssss.schemeId = ttt.id AND ttt.code = :schemeCode AND "
+ "ssss.ruleCode = :ruleCode ORDER BY ssss.effDate DESC LIMIT 1")
However, if you want to use the query as it is, then you would need to force the dialect as suggested here.
In your case, this should work:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect
From another post: unexpected token: LIMIT
Basically your query should be native:
`#Query(
"SELECT ssss FROM SSSSPackageDiscountLoad ssss, SSSBasicLoad ttt WHERE ssss.schemeId = ttt.id AND ttt.code = :schemeCode AND "
+ "ssss.ruleCode = :ruleCode ORDER BY ssss.effDate DESC FETCH FIRST 1 ROWS ONLY", nativeQuery = true)`
Notice the nativeQuery at the end.
By default, nativeQuery is false.
I want to display all the checklists that are not answered and not answered (response checklists is in the ResponsesCheckLists table) using the following parameters: idequipement and idMission.
#Query("SELECT check,resp,eq FROM Equipements eq INNER JOIN CheckLists check WHERE eq.idEquipements = check.equipements.idEquipements LEFT JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss WHERE eq.idEquipements = :idEqp ")
public List<ResponsesCheckLists> ListCheckListsNonRepondu(#Param("idEqp") long idEqp, #Param("idmiss") long idmiss);
After running this query, I displays this error message:
antlr.NoViableAltException: unexpected token: LEFT
------
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: LEFT near line 1, column 148 [SELECT check,resp,eq FROM com.SSC.DAO.Entities.Equipements eq INNER JOIN CheckLists check WHERE eq.idEquipements = check.equipements.idEquipements LEFT JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss WHERE eq.idEquipements = :idEqp ]
------
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'responsesCheckListsRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.SSC.DAO.JPARepository.ResponsesCheckListsRepository.ListCheckListsNonRepondu(long,long)!
Edit1:
#Query("SELECT check,resp,eq FROM Equipements eq INNER JOIN CheckLists check ON eq.idEquipements = check.equipements.idEquipements"
+ " INNER JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss AND eq.idEquipements = :idEqp ")
public List<ResponsesCheckLists> ListCheckListsNonRepondu(#Param("idEqp") long idEqp, #Param("idmiss") long idmiss);
Error of Edit1;
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'check' {originalText=check}
antlr.SemanticException: Path expected for join!
Caused by: java.lang.IllegalArgumentException: Validation failed for
query for method public abstract java.util.List
com.SSC.DAO.JPARepository.ResponsesCheckListsRepository.ListCheckListsNonRepondu(long,long)!
Edit2:
#Query("SELECT check , resp , eq FROM Equipements eq INNER JOIN CheckLists check ON eq.idEquipements = check.equipements.idEquipements"
+ " INNER JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss AND eq.idEquipements = :idEqp ")
public List<ResponsesCheckLists> ListCheckListsNonRepondu(#Param("idEqp") long idEqp, #Param("idmiss") long idmiss);
Errors of Edit2:
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'check' {originalText=check}
antlr.SemanticException: Path expected for join!
How to correct this query?
thank you in advance
You have a Spring annotation there (#Query) that specifies a JPQL query. Your JPQL query is supposed to follow the syntax highlighted in this link (and the JPA spec). Sadly you haven't followed that.
SELECT {result} FROM {from} WHERE {where} ...
Any "JOIN" has to go in the FROM clause. You already put one JOIN in the FROM clause, but for reasons only known to you, you decided to put another JOIN in the WHERE clause!! In fact you have 2 WHERE clauses in that crap.
It is impossible to tell you what your query should be because you don't post your entities, so we don't see what relations they have, or even what you are trying to achieve. We can only point out your error
after updating of hibernate and spring a get the following error for this query:
#NamedQuery(name = "Payment.byAmount",
query = "select p from Payment as p join p.application as a where p.amount = ?1 and p.channel = ?2 and p.type =?3 and p.created = ?4 and p.deletedDate is null and a.uuid = ?5")
Error:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'a.id' [select count(p) from de.model.Payment p join p.application a1 where p.type = .....
Any ideas how this can be solved?
Thanks in advance.
I need get all data from relative table so I'm using something like this (i would use it in sql)
private static final String SELECT_OOPR_TO_SEND = "SELECT R.* " +
"FROM offerOrderProjectRel R, offerOrder O, project P " +
"WHERE P.id = R.project_id and O.id = R.offer_order_id " +
"and O.type = 'ORDER' and (P.status = 'PENDING' or P.status ='PROTECTED')" ;
;
#SuppressWarnings("unchecked")
public List<OfferOrderProjectRel> findAllOfferOrderToSendToSalesmans() {
Query q = getSession().createQuery(SELECT_OOPR_TO_SEND);
List<OfferOrderProjectRel> list = q.list();
return list;
}
After lauching this code i'm getting that error :
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting IDENT,
found '**' near line 1, column 10 [SELECT R.* FROM offerOrderProjectRel
R, offerOrder O, project P WHERE P.id = R.project_id and O.id =
R.offer_order_id and O.type = 'ORDER' and (P.status = 'PENDING' or
P.status ='PROTECTED')]
So how can I obtain all data from column R with hibernate?
The method createQuery expects an HQL query string.
HQL is an object-oriented querying language.
HQL interprets SELECT R.* as select the member field * of the object R.
But * is not a member field of R. Is it?..
To select all the member fields of R use:
SELECT R
FROM offerOrderProjectRel R, offerOrder O, project P
WHERE P.id = R.project_id and O.id = R.offer_order_id
and O.type = 'ORDER' and (P.status = 'PENDING' or P.status ='PROTECTED')
you use SQL query, not hql query, so it should be
Query q = getSession().createSQLQuery(SELECT_OOPR_TO_SEND);
For people that received the "expecting IDENT found “*”" error when using org.springframework.data.jpa.repository.Query and found this question I'll add that you can change the nativeQuery flag to true:
#Query(value = "SELECT * FROM table1", nativeQuery = true)
List<Object> myFindAll();
I am a newbie with HQL and I am trying to do this :
select count(T) from (
select inscription, max(wrd_step) as max
from table aa
where rd_context = ?
group by inscription
) as T
where T.max = ?
The error is :
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 21 [select count(T) from( select inscription, max(wrd_step) from table aa where rd_context = ? group by inscription) as T where T.max = ?]
Thanks
EDIT :
The query in HQL is :
SELECT count(distinct inscription)
FROM Entity
WHERE inscription in (
select distinct inscription
from Entity
where rd_context = ?
group by inscription
having max(wrd_step) = ?
)
Hibernate documentation states:
Note that HQL subqueries can occur only in the select or where clauses.
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#queryhql-subqueries
But assuming table is a mapped entity (is it?), you can do this (not tested):
select count(aa)
from table aa
where rd_context = :param1
group by inscription
having max(wrd_step) = :param2