title: Save an entity and all its related entities in a single save in spring boot
body: <p>I'm using Spring Boot,REST and JPA to build my application. In app, there are 2 entities with one to many relationship.</p>
  
  <p>Entity 1 :</p>
  
  <pre><code>@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&lt;CustomReportActivity&gt; customReportActivitySet;
  
  
  
  public Set&lt;CustomReportActivity&gt; getCustomReportActivitySet() {
      return customReportActivitySet;
  }
  
  public void setCustomReportActivitySet(Set&lt;CustomReportActivity&gt; 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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Entity 2:</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And my request JSON is as follows :</p>
  
  <pre><code>{
     "name": "test report",
     "createdBy" : 129,
     "customReportActivitySet": [
          {"activityName":"a"},
          {"activityName":"b"},
          {"activityName":"c"},
          {"activityName":"d"},
          {"activityName":"e"}
      ]  
  }
  </code></pre>
  
  <p>I want to save both entities in one shot. I've implemented the save functionality in following way:</p>
  
  <pre><code>@RequestMapping(value="/save", method = RequestMethod.POST)
  public ResponseEntity&lt;?&gt; addReport(@RequestBody CustomReport customReport) {
      return new ResponseEntity&lt;&gt;(customReportService.createCustomReport(customReport), HttpStatus.CREATED);
  </code></pre>
  
  <p>}</p>
  
  <p>CustomReportService method:</p>
  
  <pre><code> public CustomReport createCustomReport(CustomReport customReport) {
      return customReportRepository.save(customReport);
  }
  </code></pre>
  
  <p>CustomRepository:</p>
  
  <pre><code>public interface CustomReportRepository extends CrudRepository&lt;CustomReport, Long&gt; {
  </code></pre>
  
  <p>}</p>
  
  <p>But I'm getting the  constraint violation exception with this:</p>
  
  <blockquote>
    <p>java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot
    insert NULL into ("REPORT_ACTIVITY"."REPORT_ID")</p>
  </blockquote>
  
  <p>Is it possible to save both entities in one save operation? </p>
  
  <p>Please help!</p>