What Entity properties, when accessed through CUBA Studio may fix this problem? If not through CUBA, what will I need to add to my Java Class for the related Entities. I guess I don’t understand what cascade PERSIST really is.
IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST:
I recognize that this question has already been addressed, but not in English. Sorry.
This exception occurs when you save (persist or merge) an object with a reference to another object which is new (does not exists in the database). To avoid the error, such new object should be saved in the same persistence context, i.e. in the same transaction.
For example, you have two related entities: Customer and Order, and Order can contain a reference to Customer. Then, you have an editor screen for Order with the corresponding datasource. Imagine that when you edit a new Order, you programmatically create a new instance of Customer and set it to Order’s attribute. After that, you commit the screen. DataManager on middleware receives a CommitContext with the new Order which has the new Customer as a reference. It invokes EntityManager.persist(order), commits the transaction, and here the exception occurs - the Customer is not passed to the persistence context and there is no CASCADE relationship between Order and Customer.
The solution is to pass the new Customer together with the new Order. It can be done automatically if the screen contains a datasource for Customer - all datasources are saved on screen commit. Alternatively, the new Customer instance can be added to CommitContext in a DsContext.BeforeCommitListener.