Unable to duplicate object

Hi. I am trying to create a copy function that should not only copy a page object itself but also the image objects related to it. I am able to copy the image objects. Also, page object can be copied as well but when combining this with the related images I get this error:

IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.company.test.entity.Image-39a64c7a-6704-9a5f-66b2-2ca787b646e9 [new].

Attached is a test project that contains the extract of my main project and that isolates the problem I am not getting to work around. There is some code for trying alternative solutions but they all end up in the same error.

Any help appreciated.

test.zip (438.7K)

Hi Berend,

When copying a graph of objects, commit all of them in one call of Middleware to save them in one transaction and one persistence context. For example, you can do it using DataManager:

CommitContext cc = new CommitContext().addInstanceToCommit(newPage);
for (Image image : myNewImages) {

The same is done behind the scene if you place all your new instances in datasources of a screen and invoke getDsContext().commit(). In this case, changes in all datasources will be saved in one Middleware call.

Excellent - this worked like a charm - very nice.