One To Many with Parent composite primary key is part of child primary key issue. 用下面的代碼片斷拋出例外
。示例JSON嵌入資料如下:
{"pt_reg_no": "1000", //序列號生成
"game_year": "G12021",
"name": "myname",
"eventDetails": [{ "major_event_code": "A", "sub_event_code": "A7", "category_code": "MO" }, { "major_event_code": "B", "sub_event_code": "B7", "category_code": "WO" } ]
}
參與者可以注冊多個事件:
參與者(復合鍵)- pt_reg_no, game_year
EventDetails (復合鍵) - pt_reg_no, game_year, sub_event_code, category_code
//Parent Class IDclass。
public class ParticipantKey implements Serializable {
private Long pt_reg_no;
private String game_year;
}
。
public class ParticipantModel {
private Long pt_reg_no;
private String game_year = 'G12021';
private Set<EventDetailsModel> eventDetails = new HashSet>()。
public Set<EventDetailsModel> getEventDetails() {
return eventDetails;
}
public void setEventDetails(Set<EventDetailsModel> eventDetails) {
this.eventDetails = eventDetails;
for (EventDetailsModel b : eventDetails) {
b.setParticipantModel(this)。
}
}
}
//子類IDclass。
public class ParticipantEventDetailsId implements Serializable {
private ParticipantKey participantModel;
private String sub_event_code;
private String category_code;
}
public class EventDetailsModel {
private String sub_event_code;
private String category_code;
private participantModel participantModel;
public ParticipantModel getParticipantModel() {
return participantModel。
}
public void setParticipantModel(ParticipantModel participantModel) {
this.participantModel = participantModel。
}
}
//---------------------------------------------------
//JSON input data received into ParticipantModel from @requestBody.
public class FormDataObjects {
private ParticipantModel formData;
public ResponseEntity<?> participantRegistration( FormDataObjects values) {
ParticipantModel participant = values.getFormData()。
participant.setGame_year(new SimpleDateFormat("yyyy"/span>)。 format(new Date()) GenricData.game)。)
ParticipantModel person = participantRepository.save(participant)。
if (person == null) {
return ResponseEntity.ok("Participant is not inserted") 。
} else {
Set<EventDetailsModel> eventDetails = participant.getEventDetails()。
if (eventDetails.size() > 0) {
eventDetails.forEach(event -> {
//event.setPt_reg_no(person.getPt_reg_no() );
//event.setGame_year(person.getGame_year() );
//event.setParticipantModel(participant);
entityManager.persist(event)。
entityManager.flush()。
entityManager.clear()。
});
}
}
return ResponseEntity.ok("inserted"/span>)。
}
uj5u.com熱心網友回復:
你可以通過像這樣映射你的細節類來嘗試一個 "派生身份":
public class ParticipantEventDetailsId implements Serializable {
private String sub_event_code;
private String category_code;
private ParticipantKey participantModel; //match name of attribute and type of ParticipantModel PK。
}
public class EventDetailsModel {
private String sub_event_code;
private String category_code;
private participantModel participantModel。
}
在JPA 2.2規范的第2.4.1節中討論了派生身份(有例子)。
uj5u.com熱心網友回復:
當父類有復合鍵且父類也需要生成序列號時,OneToMany映射的替代解決方案
在Set或List子物體中使用@Transient注解在Set或List子物體中使用@Transient注解。
1)用上述注解保存父物體有助于jpa跳過List/Set of EventsDetailsModel的保存
標籤: 上一篇:werkzeug.routing.BuildError。無法為端點'post'建立網址。你是否忘記了指定值['post_id']?
。
2) 遍歷子物體并獲得已經保存的父物體復合鍵值,并將子物體資料與子物體的鍵屬性一起持久化。
//Parent Class IDclass。
public class ParticipantKey implements Serializable {
private Long pt_reg_no;
private String game_year;
}
@IdClass(ParticipantKey.class)。
public class ParticipantModel {
@Id
@GeneratedValue(
策略 = GenerationType.SEQUENCE,
生成器 = "參與者eq"
)
@SequenceGenerator(
名稱="參與者eq"。
分配大小 = 1
)
private Long pt_reg_no;
@Id
private String game_year = 'G12021';
@Transient //Find exact path for Transient
private Set<EventDetailsModel> eventDetails = new HashSet<> ()。
public Set<EventDetailsModel> getEventDetails() {
return eventDetails;
}
}
//子類 IDclass
public class ParticipantEventDetailsId implements Serializable {
private Long pt_reg_no;
private String game_year;
private String sub_event_code;
private String category_code;
}
public class EventDetailsModel {
@Id
@Column(name = "sub_event_code")
private String sub_event_code;
@Id
@Column(name = "category_code")
private String category_code;
@Id
private Long pt_reg_no;
@Id
private String game_year;
}
//---------------------------------------------------
//JSON input data received into ParticipantModel from @requestBody
public class FormDataObjects {
private ParticipantModel formData;
@JsonCreator[/span
public FormDataObjects(ParticipantModel formData) {
this.formData = formData;
}
}
//Controller Entry point
@RequestMapping(path = "/participantData", method = RequestMethod.POST)
@Transactional
public ResponseEntity<?> participantRegistration(@Valid @RequestBody FormDataObjects values) {
ParticipantModel participant = values.getFormData()。
participant.setGame_year(new SimpleDateFormat("yyyy"/span>)。 format(new Date()) GenricData.game)。)
ParticipantModel person = participantRepository.save(participant)。
if (person == null) {
return ResponseEntity.ok("Participant is not inserted") 。
} else {
Set<EventDetailsModel> eventDetails = participant.getEventDetails()。
if (eventDetails.size() > 0) {
eventDetails.forEach(event -> {
event.setPt_reg_no(person.getPt_reg_no())。
event.setGame_year(person.getGame_year())。
entityManager.persist(event)。
entityManager.flush()。
entityManager.clear()。
});
}
}
return ResponseEntity.ok("inserted"/span>)。
}
