Open oramattkosem opened 6 months ago
Hello, the reason might be because you use long as an id. So if there is already a child with the id 0 in the database then you cannot insert a new child with the id 0 again. Changing long to Long might help because Long is nullable.
I can confirm that that appears to work as a workaround. This otherwise works properly with GenerationType of SEQUENCE as well. It's the combination of IDENTITY and primitive long that misbehaves.
I am glad, that it works. And i would change the id generator from GenerationType.IDENTITY to GenerationType.SEQUENCE if it is possible. GenerationType.SEQUENCE ist usually faster when you must insert many entities into the database.
Edit: I see, that it is not defined in the Jakarta Persistence spec what should happen when the primary key is 0. So the behavior can vary between Jakarta Persistence implementations.
Describe the bug I cannot persist a parent+children with a unidirectional OneToMany relationship if the GenerationType is IDENTITY.
To Reproduce Define a persistence unit like this:
And a parent like this:
And a child like this:
Then persist the parent with new children like this:
Expected behavior The parent and children should persist successfully, new IDs assigned to all and parent/child IDs should be populated.
Additional context Instead of using the identity generator, the records are inserted with an ID of 0. A primary key violation occurs as a result, and the graph cannot be persisted.
If the GenerationType is SEQUENCE, this works as expected.