私はコース(コースの1人の関係に多くの一部であるオブジェクト活動を作成しようとしています内のオブジェクトを作成します。複数のアクティビティを持つことができます)。春多対1の関係(非NULLプロパティが一時値を参照)
私は新しいActivityオブジェクトを永続化しようとすると、私は次のエラーを取得する:
org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : app.model.activity.Activity.course -> app.model.Course
私は、関連するコードが含まれます。
コントローラPOSTメソッド
@RequestMapping(value = "/activities", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody ResponseEntity<Void> createActivity(@Valid @RequestBody ActivityPostDto activityPostDto, UriComponentsBuilder ucBuilder) {
CourseDto courseDto = courses.findById(activityPostDto.getCourseId());
Course course = modelMapper.map(courseDto, Course.class);
if (course==null){
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
ActivityType activityType = activityService.findType(activityPostDto.getTypeId());
if (activityType==null){
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
Activity activity = new Activity();
activity.setCourse(course);
activity.setType(activityType);
activity.setName(activityPostDto.getName());
activity.setDate(new Date(activityPostDto.getDate()));
activity = activityService.add(activity);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/activities/{id}").buildAndExpand(activity.getId()).toUri());
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
ActivityPostDtoクラス
public class ActivityPostDto implements Item {
@NotNull
private Long date;
@NotNull
private String name;
@NotNull
private Long courseId;
@NotNull
private Long typeId;
...
}
アクティビティクラス
public class Activity implements Item, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@ManyToOne
@JoinColumn(name = "type_id", nullable = false)
private ActivityType type;
@ManyToOne
@JoinColumn(name = "course_id", nullable = false)
@JsonSerialize(using = CustomCourseSerializer.class)
private Course course;
...
}
のコースクラス
public class Course implements Item, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@OneToMany(mappedBy = "course", fetch = FetchType.EAGER, targetEntity = Activity.class, cascade = CascadeType.ALL)
@Fetch(value = FetchMode.SUBSELECT)
@JsonSerialize(using = CustomActivityListSerializer.class)
private List<Activity> activities;
...
}
私はこの問題について少し読んだが、私は解決策を見つけることができません。どんな助けでも大歓迎です。ありがとうございました。
あなたはCourseDtoやコースリポジトリのコードを共有することができます:あなたはこのような何かを行うことができ
? –
あなたは 'FetchMode.SUBSELECT'を削除しようとしましたか?セッション中に対応する 'Course'がない' Activitiy'をロードしているが、DB内にのみロードしているというこのブログをチェックしてください:http://www.solidsyntax.be/2013/10/17/fetch-collections-hibernate/# Hibernate_FetchMode_3A_SUBSELECT –
CourseDtoはCourseと全く同じ構造を持ちますが、注釈はありません。コースサービスはSpringが自動的に実装するPagingAndSortingRepositoryからリポジトリのfindOneメソッドを呼び出します。 –