我正在嘗試保存一個父物件->(Course)、一個子物件 ->(HoleLayout)和另一個子物件->(Hole),除了這個使用Course和HoleLayout的 id ,所有這些都同時使用后映射。
我已經嘗試了 StackOverflow 上的大部分建議,但到目前為止,我還沒有運氣。它完全保存了所有三個物體,唯一的問題是沒有將外部 id 分配給任何子物件。我不確定錯誤是否發生在我的CourseController或我的CourseServiceImpl類中。我還使用了一個列舉,它具有三個不同的變數(冠軍、男裝、女裝),當我嘗試保存課程物體時使用它。
到目前為止,我已經嘗試使用雙向映射和 JsonConverter 將Hole和HoleLayout轉換為 JSON,這不是答案。
這是我嘗試使用 Postman 保存 Course 物體時撰寫的JSON 正文。
這是父物體Course:
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@OneToMany(mappedBy = "course")
private List<Score> scores;
@OneToMany(mappedBy = "course", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<HoleLayout> holeLayout = new ArrayList<>();
@Column(name = "courseName", nullable = false)
private String courseName;
... getters and setters.
這是第一個子物體HoleLayout
@Table(name = "holeLayout")
public class HoleLayout {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@OneToMany(mappedBy = "holeLayout", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Hole> holes = new ArrayList<>();
@ManyToOne
private Course course;
@Enumerated(EnumType.ORDINAL)
@Column(name = "layoutType", nullable = false)
private LayoutType layoutType;
@Column(name = "front9Yards", nullable = false)
private long front9Yards;
@Column(name = "back9Yards", nullable = false)
private long back9Yards;
@Column(name = "overallPar", nullable = false)
private int overallPar;
@Column(name = "courseRating", nullable = false)
private double courseRating;
@Column(name = "slopeRating", nullable = false)
private double slopeRating;
... getters and setters
最后,第二個子物件Hole,它應該有 Course 和 HoleLayout id。
@Table(name = "hole")
public class Hole {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@ManyToOne
private HoleLayout holeLayout;
@Column(name = "holeNumber", nullable = false)
private int holeNumber;
@Column(name = "yards", nullable = false)
private int yards;
@Column(name = "par", nullable = false)
private int par;
... getters and setters.
這是CourseServiceImpl:
@Override
public Course createCourse(Course course) {
course.getHoleLayout().forEach(layout -> layout.setCourse(course));
for (int i = 0; i < course.getHoleLayout().size(); i ) {
HOLE_REPOSITORY.save(course.getHoleLayout().get(i).getHoles().get(i));
course.getHoleLayout()
.get(i)
.getHoles()
.forEach(hole -> hole.setHoleLayout(course.getHoleLayout().get(i)));
}
COURSE_REPOSITORY.save(course);
HOLE_LAYOUT_REPOSITORY.saveAll(course.getHoleLayout());
return course;
}
當我嘗試保存課程時
, CoursServiceImpl也會引發此錯誤:Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because the return value of "com.Rest.GolfMax.API.Models.Course.getHoleLayout()" is null
最后,這是CourseController
public ResponseEntity<CourseDto> addNewCourse(@RequestBody @NotNull CourseDto courseDto) {
Course courseRequest = modelMapper.map(courseDto, Course.class);
if (COURSE_SERVICE.isValid(courseDto.getCourseName()))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Course course = COURSE_SERVICE.createCourse(courseRequest);
CourseDto courseResponse = modelMapper.map(course, CourseDto.class);
return new ResponseEntity<>(courseResponse, HttpStatus.CREATED);
}
uj5u.com熱心網友回復:
您還需要為它們在子物體中的關系設定參考。您可以遵循以下方法:
course.getHoleLayout().forEach(layout->layout.setCourse(course));
Similarly, do it for other child:
course.getScores().forEach(score-> score.setCourse(course));
現在,一口氣保存所有內容:
COURSE_REPOSITORY.save(course);
注意:確保您的物體的主鍵值是物件形式:像這樣:
private Long id;
更新:
而不是這樣寫:
course.getHoleLayout().forEach(layout -> layout.setCourse(course));
for (int i = 0; i < course.getHoleLayout().size(); i ) {
HOLE_REPOSITORY.save(course.getHoleLayout().get(i).getHoles().get(i));
course.getHoleLayout()
.get(i)
.getHoles()
.forEach(hole -> hole.setHoleLayout(course.getHoleLayout().get(i)));
}
COURSE_REPOSITORY.save(course);
HOLE_LAYOUT_REPOSITORY.saveAll(course.getHoleLayout());
return course;
寫這個:
course.getHoleLayout().forEach(layout -> layout.setCourse(course));
COURSE_REPOSITORY.save(course);
return course;
此外,在保存它們之前,您需要確保它們的值為空。希望能奏效。
uj5u.com熱心網友回復:
更新,我通過將@JsonManagedReference 添加到我的@OneToMany 初始化并將@JsonBackReference 添加到外部ID 映射到的類來解決了這個問題。感謝user404的幫助!它沒有解決問題……但另一方面,我最終學到了一些新東西。
課程物體:
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@OneToMany(mappedBy = "course")
private List<Score> scores;
@OneToMany(mappedBy = "course", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonManagedReference
private List<HoleLayout> holeLayout = new ArrayList<>();
@Column(name = "courseName", nullable = false)
private String courseName;
... getters and setters.
孔布局:
@Table(name = "holeLayout")
public class HoleLayout {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@OneToMany(mappedBy = "holeLayout", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonManagedReference
private List<Hole> holes = new ArrayList<>();
@ManyToOne
@JsonBackReference
private Course course;
@Enumerated(EnumType.ORDINAL)
@Column(name = "layoutType", nullable = false)
private LayoutType layoutType;
@Column(name = "front9Yards", nullable = false)
private long front9Yards;
@Column(name = "back9Yards", nullable = false)
private long back9Yards;
@Column(name = "overallPar", nullable = false)
private int overallPar;
@Column(name = "courseRating", nullable = false)
private double courseRating;
@Column(name = "slopeRating", nullable = false)
private double slopeRating;
... getters and setters
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505387.html
上一篇:SpringJPA/HIbernate-堅持到H2作業正常,但Oracle失敗,ORA-01400“無法插入NULL”
