Search code examples
javamongodbjpaspring-data-jpareactive-mongo-java

What is the order of saving objects of a Model class?


I have an POJO class for a JSON as follows:

public class Portcall {

   private Long id;
   .
   .

   PreviousPortCall previousPortCall;

   NextPortCall nextPortCall;

   .
}

Here all the 3 classes-PortCall, PreviousPortCall, NextPortCall are annotated with @Document. Any change in PortCall.java will have effect on its immediate previous and next object as well which are referred by PreviousPortCall and NextPortCall In case I update the PortCall.java in Mongo DB, which order do I save the objects? Shall I save PreviousPortCall and NextPortCall and then go for saving PortCall.java or vice versa and why?


Solution

  • When you save an object of Portcall, the complete state of the object is stored into the database. So if your fields previousPortCall and nextPortCall are defined as @Entity as well, they will be stored automatically as the are part of the state of the Portcall instance. You don't need to save them explicitly at all.

    Secondly: As long as you store multiple objects within the same Transaction, the order of storing does not matter, as the complete state will be atomicly visible in the database when the transaction is committed.