Don't persist sub entity until owning entity is saved

I can see why this is occurring, but I’m not sure of how to resolve in the studio.

I have an entity say Basket.class

In Basket.class I have
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = “BASKET_ID”)
protected List fruit;

Note: Is is important to not use any of the soft-deletion functionality.

Now what I want to be able to do is create the fruit while creating the basket.

So I open a standard editor for Basket. The editor contains a table with fruit (which will be empty for a new basket). On the table I have an action and button to “Create Fruit”. Which opens up the fruit editor. So I create a new fruit.

I want to be able to create a new fruit and put it in the basket while creating the basket. And when the basket is saved to then save the fruit. It keeps trying to save the fruit first (which is obvious i guess).

I am having all sorts of problems where the fruit is saving and when I save the basket I get unique violation error as the basket has already been saved. (This is with a nestled datasource). If I used a collection datasource that is not nestled the fruit is created but not saved to the basket, or i get a jpql error about the basket not being saved.

Whats the easiest solution for this?


you’re example is exactly what a Composition is. CUBA has native support for this scenario:

I attached you an example which shows how to add fruits to the basket and only store them once the basket itself is stored. (95.2 KB)

I hope this helps.


1 Like

Thank you so much Mario. Hadn’t realised Cuba had this “composition” function. Works well!


Also please note that cascade = CascadeType.ALL annotation parameter is not needed when using CUBA composition.

1 Like