I am not able to update the child object while doing one to many relationship. When I am going to update the record, I am getting "Multiple representations of the same entity [com.aexp.grt.publisher.model.ExternalSource#163] are being merged" message.

ExternalSource.java:

@Entity
@Table(name="external_src")
public class ExternalSource extends AbstractEntity implements Serializable{

    // Variables --------------------------------------------------------Starts
    private static final long serialVersionUID = 6856225421019089955L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "external_src_id",unique = true, nullable = false)//external_src_id
    private int externalSrcId;

    @Column(name = "target_table")
    private String targetTable;

    @Column(name = "target_database")
    private String targetDatabase;

    @Column(name = "target_usecase")
    private String targetUsecase;

    @JsonManagedReference 
    @OneToMany(mappedBy = "externalSource",fetch = FetchType.EAGER,orphanRemoval = true)
    @Cascade({CascadeType.ALL}) 
    private List<ExternalSourceExecution> externalSourceExecution;
    // Variables --------------------------------------------------------Ends

    // Getter Setter -------------------
}

ExternalSourceExecution.java:

    @Entity
    @Table(name="external_src_exec")
    public class ExternalSourceExecution extends AbstractEntity implements Serializable {

        // Variables --------------------------------------------------------Starts
        private static final long serialVersionUID = -562117894266007656L;

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "external_src_exec_id")
        private int externalSrcExecId;

        @Column(name="status")
        private String status;

        @Column(name="last_refresh")
        public Timestamp lastRefresh;

        @JsonBackReference 
        @ManyToOne(targetEntity=ExternalSource.class,fetch = FetchType.EAGER)
        @JoinColumn(name = "external_src_id")
        @Cascade({CascadeType.ALL}) 
        private ExternalSource externalSource;
        // Variables --------------------------------------------------------Ends

        // Getter Setter ---------------------------------------Starts
}

Controller Class Update Method():

 @PutMapping("/updateExternalsources")
     public ResponseEntity<ExternalSource> update(@RequestBody ExternalSource externalSource) {
         ExternalSource externalSourceOld = null;
         try{
             externalSourceOld= externalSourceService.findExternalSourceById(externalSource.getExternalSrcId());

             if(externalSourceOld==null){
                      int externalSourceId = externalSource.getExternalSrcId();
                      externalSource.setExternalSrcId(externalSourceId);
                      log.error("Id " + externalSourceId + " is not existed");
                      ResponseEntity.badRequest().build();
             }else{
                 externalSourceOld.setSourceName(externalSource.getSourceName());
                 externalSourceOld.setSourceType(externalSource.getSourceType());
                 externalSourceOld.setConnectionDetails(externalSource.getConnectionDetails());
                 externalSourceOld.setExtractionDetails(externalSource.getExtractionDetails());
                 externalSourceOld.setTargetTable(externalSource.getTargetTable());
                 externalSourceOld.setTargetDatabase(externalSource.getTargetDatabase());
                 externalSourceOld.setTargetUsecase(externalSource.getTargetUsecase());
                 externalSourceOld.setModifiedBy(externalSource.getModifiedBy());
                 externalSourceOld.setModifiedDate(externalSource.getModifiedDate());
                 externalSourceOld.setExternalSourceExecution(null);
                 externalSourceOld.setExternalSourceExecution(externalSource.getExternalSourceExecution());
                 }
         }catch(Exception e){

         }

         return ResponseEntity.ok(externalSourceService.createExternalSource(externalSourceOld));
}

Postman req:

{
    "externalSrcId":163,
    "sourceName": "Updated",
    "sourceType": "Updated",
    "connectionDetails": "Updated",
    "extractionDetails": "Updated",
    "targetTable": "Updated",
    "targetDatabase": "Updated",
    "targetUsecase": "Updated",
    "createdBy": 1513295789000,
    "createdDate": 1513295789000,
    "modifiedBy": 1513295789000,
    "modifiedDate": 1513295789000,

    "externalSourceExecution":[
        {

            "status": "Updated",
            "lastRefresh": 1513295789000,
            "createdBy": 1513295789000,
            "createdDate": 1513295789000,
            "modifiedBy": "Pankaj Dagar",
            "modifiedDate": 1513295789000
        }

        ]

}

error:

"message": "Multiple representations of the same entity [com.aexp.grt.publisher.model.ExternalSource#163] are being merged. Detached: [[email protected]]; Managed: [[email protected]]; nested exception is java.lang.IllegalStateException: Multiple representations of the same entity [com.aexp.grt.publisher.model.ExternalSource#163] are being merged. Detached: [[email protected]]; Managed: [[email protected]]"

0 Answers