Я не могу обновить дочерний объект при выполнении отношения один ко многим.Когда я собираюсь обновить запись, я получаю сообщение «Объединяются несколько представлений одной и той же сущности [com.aexp.grt.publisher.model.ExternalSource # 163].
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
}
Метод обновления класса контроллера ():
@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));
}
Требование почтальона:
{
"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
}
]
}
ошибка:
"message": "Multiple representations of the same entity [com.aexp.grt.publisher.model.ExternalSource#163] are being merged. Detached: [com.aexp.grt.publisher.model.ExternalSource@fbc9a77]; Managed: [com.aexp.grt.publisher.model.ExternalSource@28ce510d]; nested exception is java.lang.IllegalStateException: Multiple representations of the same entity [com.aexp.grt.publisher.model.ExternalSource#163] are being merged. Detached: [com.aexp.grt.publisher.model.ExternalSource@fbc9a77]; Managed: [com.aexp.grt.publisher.model.ExternalSource@28ce510d]"