Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65875305/what-is-the-order-of-saving-objects-of-a-model-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...