Cascade.Persist want to save twice the parent object


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.

  1. The orderheader has a orderlines attribute which is an association and set up for composition. So the delete policy is CASCADE.
  2. 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
  3. 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…


Hi Sandor,

Are you sure you are using composition? It should work in memory, without CascadeType.PERSIST on the JPA level.

first time I used association. Because the real child entity needs to be accessed independent of the parent. (It is not an order and orderline, it is a more less a publisher rockband relation)
But after that i switch to the composition and rebuild the project and restart the server.

BTW with my original idea (Association, one-to-many) can it work at all?

Sure, one-to-many association will work if you first save the header entity. For example, you can use extendedEditWindowActions frame which shows OK & Close, OK, Cancel buttons in your header editor: just select extended checkbox in windowActions frame properties.

Thanks so I need to save first.
I think i need some tweaking so the child table action buttons are disabled when the header entity is new or dirty…
Any clue how to achieve it?


Use for example initNewItem() method. See details here:

1 Like