I have 2 entities that needed to be persisted in two different DB's:
1) MyClassMetaData - persisted on mysql via jpa+hibernate in spring (entityManager)
2) MyClassRawData - persisted on mongoDB via spring data (mongoTemplate)
There is a one To one Relation between the two entities: There is no meaning for only one entitiesto be persisted without the other. there will always be a metadata AND rawdata for each save.
My service for saving these 2 entities looks like this
@Transactional
public void saveMyClass(metadata, rawdata){
// Do Something here
this.entityManager.persist(metadata);
this.mongoTemplate.save(rawData);
}
My question is: how do i make sure that if an error occurs on this save method - a rollback will take place and for both classes?
thanks