I have a simple problem but i cannot find the right solution for it.
A simple object model: let we have an order header entity and a composition orderline entity in a “one-to-many” relationship. So for one order we can have multiple orderlines.
- The orderheader has a orderlines attribute which is an association and set up for composition. So the delete policy is CASCADE.
- The scenario is, when we create a new orderheader, and in the edit screen we have a table for lines set, we can add new lines BEFORE we save the orderheader
- The first problem was when we try to save the first line entity. We getting JPA exception complaining about the parent attribute of lines entity, thet it needs to be marked as a cascade.PERSIST. Because the JP found a new parent entity and it must be saved before the child can be saved.
So I add in the IDE in the entity class for tha order entity the required cascade=CascadeType.PERSIST annotation. And after i can save the first child entity.
But here comes the second and fatal problem. When i try to save the second child entity, the JPA try to persist the persisted parent class once again and i getting a primary field exception, and now i am lost totally.
How i can fix this?
It is not feasible to force the user to save first the empty orderheader and after that add the lines to it…