Erro em query JPQL - unexpected end of query

Olá,

Estou tentando fazer a seguinte query JPQL:

		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 );])

Alguém poderi ajudar?

Já tentou tirar o ponto-e-vírgula do final da consulta?

Já sim , só coloquei o ponto e virgula pra ver se mudava alguma coisa.

Entao deve ser outra excecao. Pq esta que vc mandou eh especificamente sobre o “;”

Tire e veja que a excecao vai mudar.

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.

E logo acima, onde estava isso:

Ainda esta do mesmo jeito?

Não, agora está assim:

line 1:40 mismatched character '<EOF>' expecting set '1'..'9'

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);  

Funcionou desta forma!! Obrigada!

Mas qual era o problema?

Usando da forma que vc comentou:

		String jpql = "from Users where username = :username";

Ocorre a seguinte exceção:

org.springframework.dao.InvalidDataAccessApiUsageException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [from Users where username = :username], line 1, column 0: unexpected token [from].
Internal Exception: NoViableAltException(32!=[197:1: document : (root= selectStatement | root= updateStatement | root= deleteStatement );]); nested exception is java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [from Users where username = :username], line 1, column 0: unexpected token [from].
Internal Exception: NoViableAltException(32!=[197:1: document : (root= selectStatement | root= updateStatement | root= deleteStatement );])

...

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [from Users where username = :username], line 1, column 0: unexpected token [from].
Internal Exception: NoViableAltException(32!=[197:1: document : (root= selectStatement | root= updateStatement | root= deleteStatement );])

Mas da primeira forma funcionou:

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.