在Spring中以多对一关系创建对象(Not-null属性引用瞬态值)

时间:2017-03-12 08:23:45

标签: java spring hibernate many-to-one transient

我正在尝试创建一个对象活动,这是与课程(一门课程可以有多项活动)的多对一关系的一部分。

当我尝试持久保存新的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 Class

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;

   ...
}

我已经读过一些关于这个问题的内容,但我找不到解决方案。任何帮助将非常感激。谢谢。

1 个答案:

答案 0 :(得分:1)

看起来,Course不包含id

您可以这样做:

// it can be changed to check existence of id
CourseDto courseDto = courses.findById(activityPostDto.getCourseId());
if (courseDto ==null){
    return new ResponseEntity(HttpStatus.BAD_REQUEST);
}

Course course = new Course();
course.setId(activityPostDto.getCourseId()) ;

Activity activity = new Activity();
activity.setCourse(course);
activity = activityService.add(activity);