Hi,
I have a problem when I try to manipulate multiple entities in one single transaction.
The model is a self referenced entity with a parent-child structure.
When I want to delete an entity within the hierarchy I have to rebind the childs to the parent of the of the actual entity.
It is all done in a service using the TransactionalDataManager.
I create a new transaction, load all the childs from the given entity and rebind the Parent-Id to the parent of the current. Then I delete the current entity.
Now, when the current entity can not be deleted, because there is a foreign key database constraint, the save-operations of the child-entities in the for-loop are not being rolled back.
In my understanding the operation should be rolled back, when the last operation is not going through?
I am using an oracleDB, and the call to the service is done from within a remove-operation of a hierarchical table. I do the complete remove-operation in the service.
public class LagerplatzDeleteServiceBean implements LagerplatzDeleteService {
@Inject
private TransactionalDataManager transactionalDataManager;
public void LinkChildsToRoot(JtLagerplatz lagerplatz){
LoadContext<JtLagerplatz> loadContextLagerplatz
= LoadContext.create(JtLagerplatz.class).setQuery(
LoadContext.createQuery("select p from roman_JtLagerplatz p where p.idLagerplatzParent = :idLagerplatz" )
.setParameter("idLagerplatz",lagerplatz)
).setView("jtLagerplatz-view");
// run all in one transaction
Transaction tx = transactionalDataManager.transactions().get();
try {
// load list of child Entities
List<JtLagerplatz> lRet = transactionalDataManager.loadList(loadContextLagerplatz);
JtLagerplatz lagParent = lagerplatz.getIdLagerplatzParent();
// manipulate dhild entities
for (JtLagerplatz l : lRet) {
l.setIdLagerplatzParent(lagParent);
transactionalDataManager.save(l);
}
// delete entity
transactionalDataManager.remove(lagerplatz);
tx.commit();
} finally {
tx.end();
}
}
}
The calling action:
@Subscribe("jtLagerplatzesTable1.remove")
public void onJtLagerplatzesTable1Remove(Action.ActionPerformedEvent event) {
JtLagerplatz curr = jtLagerplatzesHierarchicalDc.getItem();
jtLagerplatzesTable1Remove.setConfirmation(false);
dialogs.createOptionDialog()
.withCaption("Löschen des Lagerplatzes")
.withMessage("Untergeordnete Lagerplätze werden nach oben verlinkt.")
.withActions(
new DialogAction(DialogAction.Type.YES)
.withHandler(e -> {
lagerplatzDeleteService.LinkChildsToRoot(curr);
jtLagerplatzesHierarchicalDl.load();
}), // execute action
new DialogAction(DialogAction.Type.NO)
)
.show();
}
What am I doing wrong?
Regards Manfred