title: Saving multiple different entities with Spring Data Jpa
body: <p>I have 4 entities, not related to each other and corresponds to its own table. Is there any way I can save/update them with one post call? Lets say there are 4 different components in the frontend that needs to be saved in order if certain criteria is met. I need to make the process transactional, if any of them fails, none of the data should be saved in the db and need to be executed in order.</p>
  
  <p>I am using spring-boot 2.1.6.RELEASE and spring data jpa.</p>
  
  <p>I can save each one of them invidually but I do not see/know anyway I can achieve what I am looking for.</p>
  
  <p>For example:</p>
  
  <p>I can do this</p>
  
  <pre><code>public void saveA(EntityA entityA)
  aRepository.save(entityA);
  
  public void saveB(EntityB entityB)
  bRepository.save(entityB);
  
  public void saveC(EntityC entityC)
  cRepository.save(entityC);
  
  public void saveD(EntityD entityD)
  dRepository.save(entityD);
  </code></pre>
  
  <p>On my desperation, I was trying the following which obviously didnt work:</p>
  
  <pre><code>@org.springframework.transaction.annotation.Transactional(propagation=Propagation.REQUIRED, rollbackFor = Exception.class)
  public void save(EntityA entityA,EntityB entityB,EntityC entityC,EntityD entityD)throws Exception{
  saveA(entityA);
  saveB(entityB);
  saveC(entityC);
  saveD(entityD);
  }
  </code></pre>
  
  <p>If anyone can provide me an example, description or point me to the correct direction. I would really appreciate it.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>here is my service </p>
  
  <pre><code>@Autowired
      EntityARepository aRepository;
  
      @Autowired
      EntityBRepository bRepository;
  
      @Autowired
      EntityCRepository cRepository;
  
      @Autowired
      EntityDRepository dRepository;
  
      public void saveA(List&lt;EntityA&gt; entityA) {
          aRepository.saveAll(entityA);
      }
  
      public void saveB(List&lt;EntityB&gt; entityB) {
          bRepository.saveAll(entityB);
      }
  
      public void saveC(List&lt;EntityC&gt; entityC) {
          cRepository.saveAll(entityC);
      }
  
      public void saveD(List&lt;EntityD&gt; entityD) {
          dRepository.saveAll(entityD);
      }
  
      @org.springframework.transaction.annotation.Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
      public void save(DemoBulkSave demoBulkSave) {
          saveA(demoBulkSave.getEntityA());
          saveB(demoBulkSave.getEntityB());
          saveC(demoBulkSave.getEntityC());
          saveD(demoBulkSave.getEntityD());
  
      }
  </code></pre>
  
  <p>POJO</p>
  
  <pre><code>@Entity
  @Table(name = "a")
  public class EntityA {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", updatable = false, insertable = false)
      private Integer id;
      @Column(name = "first_name")
      private String firstName;
      @Column(name = "last_name")
      private String lastName;
      @Column(name = "career")
      private String career;
  
  
  public class DemoBulkSave {
          List&lt;EntityA&gt; entityA;
          List&lt;EntityB&gt; entityB;
          List&lt;EntityC&gt; entityC;
          List&lt;EntityD&gt; entityD;
  
          public DemoBulkSave() {
          }
  
          public List&lt;EntityA&gt; getEntityA() {
              return entityA;
          }
  
          public void setEntityA(List&lt;EntityA&gt; entityA) {
              this.entityA = entityA;
          }
  
          public List&lt;EntityB&gt; getEntityB() {
              return entityB;
          }
  
          public void setEntityB(List&lt;EntityB&gt; entityB) {
              this.entityB = entityB;
          }
  
          public List&lt;EntityC&gt; getEntityC() {
              return entityC;
          }
  
          public void setEntityC(List&lt;EntityC&gt; entityC) {
              this.entityC = entityC;
          }
  
          public List&lt;EntityD&gt; getEntityD() {
              return entityD;
          }
  
          public void setEntityD(List&lt;EntityD&gt; entityD) {
              this.entityD = entityD;
          }
  
      }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>@Autowired
      private EntityService entityService;
  
      @PostMapping("demo/save")
      public void save(@Valid @RequestBody DemoBulkSave demoBulkSave) throws Exception {
          entityService.save(demoBulkSave);
      }
  </code></pre>
  
  <p>Here is my input Json</p>
  
  <pre><code>[
      {
          "firstName": "FName1",
          "lastName": "LName1",
          "career": "career1"
      },
      {
          "firstName": "FName2",
          "lastName": "LName2",
          "career": "career2"
      },
      {
          "firstName": "FName3",
          "lastName": "LName3",
          "career": "career3"
      },
      {
          "firstName": "FName4",
          "lastName": "LName4",
          "career": "career4"
      }
  ]
  </code></pre>
  
  <p>I am getting the following error while trying to save all 4 entities:</p>
  
  <pre><code>"org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.example.demo.controller.EntityController$DemoBulkSave` out of START_ARRAY token
  </code></pre>
  
  <p>UPDATE 2
  correct POST JSON format</p>
  
  <pre><code>{
      "entityA": [
          {
              "firstName": "FName1",
              "lastName": "LName1",
              "career": "career1"
          }
      ],
      "entityB": [
          {
              "firstName": "FName2",
              "lastName": "LName2",
              "career": "career2"
          }
      ],
      "entityC": [
          {
              "firstName": "FName3",
              "lastName": "LName3",
              "career": "career3"
          }
      ],
      "entityD": [
          {
              "firstName": "FName4",
              "lastName":"LName4" ,
              "career": "career4"
          }
      ]
  }
  </code></pre>