0

My problem is that I have a query with EntityManager like:

Query query = em.createQuery("select nombre from OTP_OPERACION_DETALLE");

and Java throws this exception:

error e: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select nombre from OTP_OPERACION_DETALLE], line 0, column -1: unexpected end of query.
Internal Exception: MismatchedTokenException(-1!=80)

I try this run this query in Oracle (PL/SQL) and I don't have any problem.

Where is the problem?

Lucky
  • 16,787
  • 19
  • 117
  • 151
jorgeregidor
  • 208
  • 2
  • 13
  • Can you try `em.createQuery("select nombre from OTP_OPERACION_DETALLE nombre");` ? – Lucky Aug 28 '15 at 09:14
  • I have an error to: error e: An exception occurred while creating a query in EntityManager: Exception Description: Error compiling the query [select nombre from OTP_OPERACION_DETALLE nombre]. Unknown entity type [OTP_OPERACION_DETALLE]. – jorgeregidor Aug 28 '15 at 09:29
  • Its because you may have not added the `@Entity` and not mentioned the class in your persistence.xml. Add `@Table` annotation to your `@Entity` and Also add entity to `persistence.xml`. If its not an entity just use the `em.createNativeQuery("select nombre from OTP_OPERACION_DETALLE nombre");` – Lucky Aug 28 '15 at 09:31
  • thanks!!! I used creativeNativeQuery, and all is correct!!! – jorgeregidor Aug 28 '15 at 10:03

3 Answers3

1

What you have (or rather don't have) is a JPQL query, it wouldn't run in Oracle, since only a JPA implementor can translate it to SQL. Conversely what runs in Oracle can't be run with createQuery, it would require createNativeQuery.

A correct JPQL query would be something like select a.nombre from MyEntity a where MyEntity is the entity class that maps to the OTP_OPERACION_DETALLE table (and it has a nombre field).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

First you were missing the nombre after the OTP_OPERACION_DETALLE which is the alias for the table. And as I've already mentioned in the comments, you need to check if the class used in the query is an Entity and you have used the @Table annotation in the @Entity. In your case the OTP_OPERACION_DETALLE is not an Entity so you need to use em.createNativeQuery

Query query = em.createNativeQuery("select a.nombre from OTP_OPERACION_DETALLE a");

which roughly equals to this query select nombre from OTP_OPERACION_DETALLE.

Community
  • 1
  • 1
Lucky
  • 16,787
  • 19
  • 117
  • 151
-1

Just try this war

Query q=getEntityManager().createQuery("select nombre from OTP_OPERACION_DETALLE");
List roles=q.getResultList();
if (roles.isEmpty()) {
    return null;
}
else {
    //Fetch the Record
}
SaviNuclear
  • 886
  • 1
  • 7
  • 19