Search code examples
hibernatejpaeclipselink

Exception Description: The primary key read from the row [DatabaseRecord during the execution of the query was detected to be null


Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.QueryException

Exception Description: The primary key read from the row [DatabaseRecord( => 2)] during the execution of the query was detected to be null. Primary keys must not contain null. Query: ReadAllQuery(referenceClass=ResourceHistory sql="SELECT count(1) FROM omm_logs.resource_history e where e.user_id='f6748e11-6a76-4448-9bf5-af4da5643542' and (e.datetime >= '2013-05-15 16:29:24' or e.datetime <= '2013-06-05 10:32:25' or e.service_code LIKE '%5%' or e.keyword LIKE '%1%' or e.resource_type LIKE '%1%' or e.channel LIKE '%1%')")

Query cQry=em.createNativeQuery(query,ResourceHistory.class);
long count=(Long)cQry.getSingleResult();

I am using netbeans 7.0.1, mysql 5.x.


Solution

  • If all you want is data, do not specify an entity class as a parameter to the creatNativeQuery method. Doing so forces JPA to attempt to build that entity from the results, which gives an exception when the entity data isn't there to build it with.