String jpql = "SELECT u FROM Users u WHERE u.username=?;";
Query query = getEntityManager().createQuery(jpql);
query.setParameter(1, username);
Estou tendo a seguinte exception:
line 1:40 mismatched character ';' expecting set '1'..'9'
org.springframework.dao.InvalidDataAccessApiUsageException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [SELECT u FROM Users u WHERE u.username=?;], line 0, column -1: unexpected end of query.
Internal Exception: NoViableAltException(-1!=[797:1: comparisonExpressionRightOperand returns [Object node] : (n= arithmeticExpression | n= nonArithmeticScalarExpression | n= anyOrAllExpression );]); nested exception is java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [SELECT u FROM Users u WHERE u.username=?;], line 0, column -1: unexpected end of query.
Internal Exception: NoViableAltException(-1!=[797:1: comparisonExpressionRightOperand returns [Object node] : (n= arithmeticExpression | n= nonArithmeticScalarExpression | n= anyOrAllExpression );])
...
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [SELECT u FROM Users u WHERE u.username=?;], line 0, column -1: unexpected end of query.
Internal Exception: NoViableAltException(-1!=[797:1: comparisonExpressionRightOperand returns [Object node] : (n= arithmeticExpression | n= nonArithmeticScalarExpression | n= anyOrAllExpression );])
...
Caused by: Exception [EclipseLink-8028] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [SELECT u FROM Users u WHERE u.username=?;], line 0, column -1: unexpected end of query.
Internal Exception: NoViableAltException(-1!=[797:1: comparisonExpressionRightOperand returns [Object node] : (n= arithmeticExpression | n= nonArithmeticScalarExpression | n= anyOrAllExpression );])
Não, tirei o ponto-e-virgula e a exceção parece a mesma:
org.springframework.dao.InvalidDataAccessApiUsageException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [SELECT u FROM Users u WHERE u.username=?], line 0, column -1: unexpected end of query.
Isso esta estranho… Soh pra tentar isolar o problema, mude seu codigo para algo assim:
String jpql = "SELECT u FROM Users u WHERE u.username = :username";
// String jpql = "from Users where username = :username"; // se o acima ainda der erro, tenta este, soh para testar...
Query query = getEntityManager().createQuery(jpql);
query.setParameter("username", username);
No JPQL, toda consulta válida deve começar com SELECT.
No HQL, que é a linguagem de consulta do Hibernate, é permitido omitir a cláusula SELECT. O erro aconteceu porque você está usando EclipseLink, e não Hibernate.