Whats wrong with the SQL Syntax? - java

Hey guys I've got a Problem with a JPA Query.
Here the Code-Snippet:
TypedQuery<ViVertragDl> q = entityManager.createQuery("select * from ViVertragDl c where c.id = " + editContract.getId(), ViVertragDl.class);
List<ViVertragDl> jpaContractDls = q.getResultList();
And the Glassfish Log throws this Error, after testing in SOAP-UI:
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select * from ViVertragDl c where c.id = 51].
[28, 28] A select statement must have a FROM clause.
[7, 7] The left expression is missing from the arithmetic expression.
[9, 27] The right expression is not an arithmetic expression.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
... 91 more
It Just says "A select statement must have a FROM Clause.", but in my opinion there is one.

I think you should try:
select c from ViVertragDl c where c.id = " + editContract.getId()
http://www.objectdb.com/java/jpa/query/jpql/select

Related

Error in derby database after move to java 8 time in eclipse link jpa

i have query written in JPQL and eclipse link and derby database
"select sum(LedgerDetailTable.debitValue) - sum(LedgerDetailTable.creditValue) from LedgerDetailTable as LedgerDetailTable left join LedgerMasterTable as LedgerMasterTable on LedgerDetailTable.ledgerMasterId = LedgerMasterTable.id left join ReferenceTable as ReferenceTable on LedgerMasterTable.referenceId = ReferenceTable.id where LedgerDetailTable.assistantAccountId = 1 AND ReferenceTable.workDateTime <" + "'" + workDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "'"
i got error
Caused by: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "T19" at line 1, column 275.
the generated sql from eclipse link log
SELECT (SUM(t0.debitValue) - SUM(t0.creditValue)) FROM LedgerDetailTable t0 LEFT OUTER JOIN LedgerMasterTable t1 ON (t0.ledgerMasterId = t1.id) LEFT OUTER JOIN ReferenceTable t2 ON (t1.referenceId = t2.id) WHERE ((t0.assistantAccountId = 1) AND (t2.workDateTime < 2020-02-27T19:41:52))
why java converted '2020-02-27 19:41:52' to 2020-02-27T19:41:52 with out single quates
the problem fixed by upgrade eclipselink 2.7 to 2.7.6

"FOR UPDATE NOWAIT", Oracle, springframework, QuerySyntaxException

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

Error on use having with count in querydsl

I trying use querydsl query with having clause with count aggregation but i have the error
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: having near line 4, column 1 [select count(distinct logData.ipAddress)from com.ef.domain.LogData logData where logData.date between ?1 and ?1 having count(logData) >= ?2]
By the following code
return query.select(Projections.constructor(LogData.class, data.count(), data.ipAddress)).from(data).where(data.date.between(args.getStartDate(), args.getFinalDate()))
.groupBy(data.ipAddress).having(data.count().goe(args.getThreshold())).fetchResults().getResults();
What´s wrong?

JPA,JPQL: unexpected token: LEFT near line 1

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

The field in this expression has an invalid table in this context

i'm trying to do following query:
#NamedQuery(name="Grade.findGradeByClassGroup",
query= "SELECT gr From Grades gr join DeterminateTable d on gr.DeterminateTableId = d.DeterminateTableId join SchoolYears sy on gr.GradeId = sy.GradeId join ClassGroups cg on cg.schoolYear = sy.YearInt where cg.groupId = :classGroup")
And I get this exception:
Caused by: Exception [EclipseLink-6069] (Eclipse Persistence Services - 2.6.0.v20141202-3914740): org.eclipse.persistence.exceptions.QueryException
Exception Description: The field [Grades.GradeId] in this expression has an invalid table in this context.
Query: ReadAllQuery(name="Grade.findGradeByClassGroup" referenceClass=Grade jpql="SELECT gr From Grades gr join DeterminateTable d on gr.DeterminateTableId = d.DeterminateTableId join SchoolYears sy on gr.GradeId = sy.GradeId join ClassGroups cg on cg.schoolYear = sy.YearInt where cg.groupId = :classGroup")
at org.eclipse.persistence.exceptions.QueryException.invalidTableForFieldInExpression(QueryException.java:755)
at org.eclipse.persistence.internal.expressions.FieldExpression.validateNode(FieldExpression.java:325)
at org.eclipse.persistence.expressions.Expression.normalize(Expression.java:3291)
at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:369)
at org.eclipse.persistence.internal.expressions.FieldExpression.normalize(FieldExpression.java:225)
at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:224)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:574)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:865)
at org.eclipse.persistence.expressions.ExpressionBuilder.normalize(ExpressionBuilder.java:267)
at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:363)
at org.eclipse.persistence.internal.expressions.FieldExpression.normalize(FieldExpression.java:225)
at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:232)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:574)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:865)
at org.eclipse.persistence.expressions.ExpressionBuilder.normalize(ExpressionBuilder.java:267)
at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:363)
at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:758)
at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:671)
at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:224)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:574)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1474)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:550)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1722)
at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:867)
at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:798)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:666)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:909)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:615)
at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:342)
... 65 more
I can't figure out whats wrong, tried to google but couldn't get any answers...
What could be causing this?

Categories