0

I have composite primary key where one of the keys is null. What annotations must be present in the entity so that I can get data using JPA of this kind:

ID_FIRST_KEY    |ID_SECOND_KEY  |DATA
-------------------------------------
1               |100            |data1
1               |200            |data2
1               |null           |data3
2               |100            |data4
......................................

I tried to use IdClass Annotation and EmbeddedId Annotation but I get exception:

   Exception Description: The primary key read from the row [ArrayRecord(
        DATA.ID_FIRST_KEY => 1
        DATA.ID_SECOND_KEY => null
        DATA.DATA => data3)] during the execution of the query was detected to be null.  Primary keys must not contain null.

The data was used for example, but similar data is used in a real project, so there is no way to replace null with a value.

1 Answers1

0

You cannot make any primary key null,If your value is null then It should not be part of primary key.Instead you can add one new column for primary key

For more detail: NULL value in multi-column primary key

Sujit Sharma
  • 104
  • 8