如何同时持久化两个相互关联的对象?

How persist two objects related to each other at the same time?

我正在尝试使用新的 JobAdTasks 对象列表来保存一个新的 JobAdvertise 对象。广告包含与其相关的任务,两者同时成功持久化。但是,每个 JobAdTask 的外键是空的。如何解决这个问题,在双向映射关系中同时持久化两个对象?

JobAdvertise class:

@JsonManagedReference
@OneToMany(cascade = CascadeType.ALL,mappedBy = "jobAdvertise")
private List<JobAdTask> taskList = new ArrayList<>();

public void addTask(JobAdTask adTask) {
   if (adTask != null) {
      if (taskList == null) { taskList = new ArrayList<>(); }
      taskList.add(adTask);
   }
}

JobAdTask class:

@JsonBackReference
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
@JoinColumn(name = "job_add_id")
private JobAdvertise jobAdvertise;

JobAdvertiseService 服务 class:

JobAdvertise jobAdvertise = new JobAdvertise();

for (String task : jobAdCreation.getTaskList()) {
   JobAdTask jobAdTask = new JobAdTask();
   jobAdTask.setTask(task);
   jobAdTaskRepo.save(jobAdTask);
   
   //Relationship JobAdvertise and JobAdTask
   jobAdvertise.addTask(jobAdTask);
}

jobAdvertiseRepo.save(jobAdvertise);

在您的服务中,您需要更改为以下内容:

for (String task : jobAdCreation.getTaskList()) {
   JobAdTask jobAdTask = new JobAdTask();
   jobAdTask.setTask(task);
   jobAdTask.setJobAdvertise(jobAdvertise);
   jobAdTask = jobAdTaskRepo.save(jobAdTask); // Also re-assign the jodAdTask variable since otherwise the id and any timestamps managed by the ORM won't be available in your object.
   
   //Relationship JobAdvertise and JobAdTask
   jobAdvertise.addTask(jobAdTask);
}

一个选项是re-write代码如下:

// in JobAdvertise
public void addTask(JobAdTask adTask) {
   if (adTask != null) {
      adTask.setJobAdvertise(this); // Link the objects here
      if (taskList == null) { taskList = new ArrayList<>(); }
      taskList.add(adTask);
   }
}

public void addTasks(Collection<JobAdTask> adTasks) { // add this method to enable passing a collection to the object, removing the need for iteration from the service class.
   adTasks.forEach(this::addTask);
}

// in JobAdTask create a constructor taking a string

public JobAdTask(String task) {
   this.setTask(task);
}

// in JobAdvertiseService
List<JobAdTask> jobAdTasks = jobAdCreation.getTaskList().stream().map(JobAdTask::new).collect(Collectors.toList()); // creates list of jobAdTasks using the constructor taking a String argument

JobAdvertise jobAdvertise = new JobAdvertise();
jobAdvertise.addTasks(jobAdTasks); // the addTasks uses the addTask method of jobAdvertise, which links the JobAdvertise object and each JobAdTask object.
jobAdvertise = jobAdvertiseRepo.save(jobAdvertise); // when you save the JobAdvertise, since you use CascadeType.ALL, both the advertise and each task object will be persisted to the database.