Datacontext merge seems not working

Hi,
We have 2 entities ( Direccio and Tipusvia ) in association relationship many (Direccio ) to one (Tipusvia). When we create a new entity Direccio in editor screen need assign Tipusvia entity programatically in BeforeCommitChanges event with this code :
// Per al cas de adreces lliures
if (getEditedEntity().getC_tipus_direccio().getId() == 2) {
if (getEditedEntity().getC_tipus_via() == null) {
Integer tipusViaId = 0;
TipusVia tipusVia = dataManager.load(TipusVia.class).id(tipusViaId).one();
TipusVia tipusViaMerged = dataContext.merge(tipusVia);
getEditedEntity().setC_tipus_via(tipusViaMerged);
}
getEditedEntity().setD_cod_pos(" “);
getEditedEntity().setD_nom_mun(” “);
getEditedEntity().setD_nom_pro(” ");
}
But this code raise exception "IllegalStateException: During syncronization a new object was found through a relationship that was not marked cascade PERSIST: org.powel.goal4.gester.entity.TipusVia-0[detached].

Please try to reproduce your problem in a small test project with entities named like Foo, Bar, etc.
And use triple quotes when posting code here on the forum. Your code in the message above is almost impossible to read.

Small test project that reproduce our problem.
test.zip (303.5 KB)

Captura

Thank you for the test project.

The problem is not related to DataContext, it occurs because the Bar id equals to 0, and EclipseLink considers entity new if it has integer id=0.
Make sure your entity identifiers start from 1. For example, you can define a proper sequence in the database and use it in the @JmixGeneratedValue(sequenceName = "my_seq") annotation.

Thanks Konstantin, now all works fine.!