在Spring Boot中一次保存一个实体及其所有相关实体

Man*_*ha 5 java spring jpa one-to-many spring-boot

我正在使用Spring Boot,REST和JPA构建我的应用程序。在应用程序中,有2个具有一对多关系的实体。

实体1:

@Entity
@Table( name = "report")
@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomReport {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REPORT_SEQ")
@SequenceGenerator(sequenceName = "REPORT_SEQ", allocationSize = 1, name = "REPORT_SEQ")
private Long id;

private String name;
private Long createdBy;
private Timestamp lastModifiedTimestamp;


@OneToMany(mappedBy = "customReport", cascade = CascadeType.ALL)
private Set<CustomReportActivity> customReportActivitySet;



public Set<CustomReportActivity> getCustomReportActivitySet() {
    return customReportActivitySet;
}

public void setCustomReportActivitySet(Set<CustomReportActivity> customReportActivitySet) {
    this.customReportActivitySet = customReportActivitySet;
}



public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Long getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(Long createdBy) {
    this.createdBy = createdBy;
}

public Timestamp getLastModifiedTimestamp() {
    return lastModifiedTimestamp;
}

public void setLastModifiedTimestamp(Timestamp lastModifiedTimestamp) {
    this.lastModifiedTimestamp = lastModifiedTimestamp;
}
Run Code Online (Sandbox Code Playgroud)

}

实体2:

@Entity
@Table( name = "report_activity")
public class CustomReportActivity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REPORT_ACTIVITY_SEQ")
@SequenceGenerator(sequenceName = "REPORT_ACTIVITY_SEQ", allocationSize = 1, name = "REPORT_ACTIVITY_SEQ")
private Long id;

String activityName;

@ManyToOne
@JoinColumn( name="report_id" )
@JsonBackReference
private CustomReport customReport;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getActivityName() {
    return activityName;
}

public void setActivityName(String activityName) {
    this.activityName = activityName;
}

public CustomReport getCustomReport() {
    return customReport;
}

public void setCustomReport(CustomReport customReport) {
    this.customReport = customReport;
}
Run Code Online (Sandbox Code Playgroud)

}

我的请求JSON如下:

{
   "name": "test report",
   "createdBy" : 129,
   "customReportActivitySet": [
        {"activityName":"a"},
        {"activityName":"b"},
        {"activityName":"c"},
        {"activityName":"d"},
        {"activityName":"e"}
    ]  
}
Run Code Online (Sandbox Code Playgroud)

我想一次性保存两个实体。我已经通过以下方式实现了保存功能:

@RequestMapping(value="/save", method = RequestMethod.POST)
public ResponseEntity<?> addReport(@RequestBody CustomReport customReport) {
    return new ResponseEntity<>(customReportService.createCustomReport(customReport), HttpStatus.CREATED);
Run Code Online (Sandbox Code Playgroud)

}

CustomReportService方法:

 public CustomReport createCustomReport(CustomReport customReport) {
    return customReportRepository.save(customReport);
}
Run Code Online (Sandbox Code Playgroud)

CustomRepository:

public interface CustomReportRepository extends CrudRepository<CustomReport, Long> {
Run Code Online (Sandbox Code Playgroud)

}

但是我得到了违反约束的异常:

java.sql.SQLIntegrityConstraintViolationException:ORA-01400:无法将NULL插入(“ REPORT_ACTIVITY”。“ REPORT_ID”)

是否可以在一个保存操作中保存两个实体?

请帮忙!

Mac*_*ski 5

你将不得不加一小段代码,这将填充每个CustomReportActivity的内CustomReport实例。只有这样持久化提供才能成功执行级联保存操作:

public CustomReport createCustomReport(CustomReport customReport) {
   customReport.getCustomReportActivitySet.forEach((activity) -> {
      activity.setCustomReport(customReport);
   });

   return customReportRepository.save(customReport);
}
Run Code Online (Sandbox Code Playgroud)

底线是必须在关系的双方设置依赖关系。