Dilemma with cascading


in my project, I have coded a callable method through the JMX console, in charge of migrating data from an external database, to the main datastore in my cuba application. In this task, I create jdbc connections and read this external db which is the source I use to populate my entity instances, which are among them interrelated, so there are instance references at many points. After completion of each record I store the info in the main datastore.

In previous projects I always used to include cascading annotations to persist data, and everything worked fine. Now using CUBA, at some point (when implementing EntityListeners), I was discouraged to use such annotations, then they prevent EntityListeners from being called.

My problem is that if I remove those annotations, I am not being able to import the data in the main datastore, then I get synchronization errors constantly of the form:

java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST

I try to merge the affected instances before commiting the main transaction with no luck. Have also tried persisting but doesn’t work either.

In a previous post I asked what DataImport strategies were more recommendable, and I was pointed to the implementation of a JMX bean method. But through this method, I am not being able to solve the above mentioned, without annotating the entities with the cascade options.

Any alternatives?




Avoiding problems with cascade persist when doing import is simple in my opinion.
Just create the collection: List<Entity> allEntitiesToPersist, pass it everywhere to all methods which create new entities and put all new entities to this collection.
After the importing is ready and you need to save data to the DB, just call dataManager.commit(new CommitContext(allEntitiesToPersist)) by passing gathered collection of entities.

All you need to do - it remember all new created entities, and pass them to the dataManager.commit() to be commited in the same transaction.

Thanks Alex for your suggestion. This was posing a considerable conflict. If I am able to keep things as simple as you suggest, that will be fine.

Will come back with results.