I’m trying to sort a resultset using the SQL statement Order by using JPA, on a datetime column data type with this string, on a Mysql database:
Query query = em.createQuery("SELECT e FROM Events e Order by e.EventDateTime;");
Using the createQuery method java returns the error:
SEVERE: Local Exception Stack:
Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):
org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [Events.findByGameId: SELECT e FROM Events e WHERE e.gameId =
:gameId ORDER BY e.EventDateTime DESC], line 1, column 59: unknown state or association field [EventDateTime] of class [com.jogogestao.entity.Events].
at org.eclipse.persistence.exceptions.JPQLException.unknownAttribute(JPQLException.java:457)
at org.eclipse.persistence.internal.jpa.parsing.DotNode.validate(DotNode.java:88)
at org.eclipse.persistence.internal.jpa.parsing.OrderByItemNode.validate(OrderByItemNode.java:52)
at org.eclipse.persistence.internal.jpa.parsing.OrderByNode.validate(OrderByNode.java:61)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:210)
I tried sorting by the integer type primary and all runs ok…but this is not what I want of course.
Using createNativeQuery the statement runs ok…
Query query = em.createNativeQuery("SELECT * FROM Events Order by EventDateTime;");
The only problem is that the return object is not an Events type object (from the entity) and I can’t convert to this type.
Maybe the problem is that JPA does not support sorting on datetime fields?
How can I get around this?
I’m using Netbeans 7.0.1, Glassfish 3.1.1, MySql 5.5.19 Community Server (GPL) and mysql-connector-java-5.1.15-bin.jar.
Thanks!
*– you have to specify the entity you selectEventvsEvents)