2017-05-30 68 views
1

我使用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; 
} 

}

實體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; 
} 

}

而我請求JSON如下:

{ 
    "name": "test report", 
    "createdBy" : 129, 
    "customReportActivitySet": [ 
     {"activityName":"a"}, 
     {"activityName":"b"}, 
     {"activityName":"c"}, 
     {"activityName":"d"}, 
     {"activityName":"e"} 
    ] 
} 

我想一次保存兩個實體。我已經實現了保存功能在以下方式:

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

}

CustomReportService方法:

public CustomReport createCustomReport(CustomReport customReport) { 
    return customReportRepository.save(customReport); 
} 

CustomRepository:

public interface CustomReportRepository extends CrudRepository<CustomReport, Long> { 

}

乙UT我得到的約束違反異常與此:

java.sql.SQLIntegrityConstraintViolationException:ORA-01400:不能 將NULL插入

是它( 「REPORT_ACTIVITY」 「REPORT_ID」。)可以將兩個實體保存在一個保存操作中?

請幫忙!

+0

whats inside customReportService.createCustomReport? –

+0

@MaciejKowalski我編輯了我的帖子。請檢查 – Manisha

+0

請參閱相關的帖子:https://stackoverflow.com/questions/3927091/save-child-objects-automatically-using-jpa-hibernate –

回答

2

您將不得不添加一小段代碼來填充每個女士的CustomReport實例。只有這樣,持久性提供才能成功執行級聯保存操作:

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

    return customReportRepository.save(customReport); 
} 

底線是必須在關係的兩側設置依賴關係。

+0

根據文檔,CrudRepository.save(實體)保存以及更新實體。但是當我使用save()更新自定義報表實體時,我的主鍵約束report_activity表被違反。report_activity表的主鍵是(report_id,activity_name)。看起來JPA嘗試在report_activity表中插入新行,而不是更新現有行。請告訴我爲什麼它沒有進行合併操作 – Manisha

+0

是的,這種隱式合併存在問題。我個人不得不通過自己的合併操作來實施。 –

+0

哦,好的。我期待JPA處理相關實體的合併。感謝您的澄清! – Manisha