Search code examples
javasql-serverjpaeclipselink

EclipseLink - The primary key read from the row during the execution of the query was detected to be null. Primary keys must not contain null


  • One Entity that has a primary key defined in two fields of table File, this is the code:

    @Id
    @Column(name = "\"ID_ORGANIZATION\"")
    private java.math.BigInteger idOrganization;
    @Id
    @Column(name = "\"Id\"")
    private java.math.BigInteger id;
    
  • This is the query :

    "SELECT "Id","FileName" FROM "File" WHERE "Id" IN (SELECT "IdFile" FROM "DocumentFile" WHERE "IdDocument"=? AND ID_ORGANIZATION=?) AND ID_ORGANIZATION=? ORDER BY "Id""
    

Supposed that the parameters are correct complete with the correct value - This is the exception receivd "The primary key read from the row [ArrayRecord( File.Id => 10228 File.FileName => 000350.pdf)] during the execution of the query was detected to be null. Primary keys must not contain null."

You can see that the primary key field of File Entity is not null (File.Id => 10228). In the database the table hasn't got any field of the primary key null(obviously).

I just tried to set the parameter "eclipselink.jpa.uppercase-column-names" = true in the configuration file ma it still doesn't work.

I am using Apache Tomcat 8, Java 8 and SqlServer 14. Eclipselink version is 2.4.0 and JPA version is 2.0.4 .

Thanks.


Solution

  • The problem is in the numerated select because the Entity File has got a primary key with two fields and EclipseLink(JPA) wants at least in the select result the two fields of the primary key.

    The select that I wrote only answer to table one of the two fields of the key and for that reason JPA can't build the object File.

    For info, the exception was thrown by this code :

    (List<File>) nativeQuery.getResultList();
    

    in the cast to object File JPA said me that the primary key is null beacause he has got only one of thw two fields.