如何在hibernate中正确映射一对一?

如何在hibernate中正确映射一对一?,hibernate,spring-boot,Hibernate,Spring Boot,我正在使用SpringBoot和hibernate制作RESTAPI。基本上,这是一个目标跟踪应用程序,我们有目标、要实现目标的里程碑以及固定数量的里程碑状态,如“已完成”、“正在进行”、“等待”。目前,我在加入状态和里程碑实体方面存在问题。如何在测试时通过写入status_id来设置状态 这是 里程碑模型 @Entity @Table(name = "milestones") @EntityListeners(AuditingEntityListener.class) public clas

我正在使用SpringBoot和hibernate制作RESTAPI。基本上,这是一个目标跟踪应用程序,我们有目标、要实现目标的里程碑以及固定数量的里程碑状态,如“已完成”、“正在进行”、“等待”。目前,我在加入状态和里程碑实体方面存在问题。如何在测试时通过写入status_id来设置状态

这是

里程碑模型

@Entity
@Table(name = "milestones")
@EntityListeners(AuditingEntityListener.class)
public class Milestone implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Size(max = 300)
private String milestoneDescription;

@NotBlank
@Size(max = 300)
private String measureDescription;


@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date dueDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "goal_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Goal goal;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "status_id")
private Status status;
//为了简洁起见,省略了构造函数、getter和setter

状态模型

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(length = 60)
private String name;

@OneToOne(fetch = FetchType.LAZY,
        cascade =  CascadeType.ALL,
        mappedBy = "status")
private Milestone milestone;
里程碑控制器

@RestController
public class MilestoneController {

@Autowired
private MilestoneRepository milestoneRepository;

@Autowired
private GoalRepository goalRepository;

@Autowired
private StatusRepository statusRepository;


@GetMapping("/goals/{goalId}/milestones")
public Page<Milestone> getAllMilestonesByPostId(@PathVariable(value = "goalId") Long goalId,
                                              Pageable pageable) {
    return milestoneRepository.findByGoalId(goalId, pageable);
}

@PostMapping("/goals/{goalId}/milestones")
public Milestone createMilestone(@PathVariable (value = "goalId") Long goalId,
                             @Valid @RequestBody Milestone milestone) {
    return goalRepository.findById(goalId).map(goal -> {


//milestone.setStatus(statusRepository.findById(milestone.getStatus().getId()));
            milestone.setGoal(goal);
            return milestoneRepository.save(milestone);
        }).orElseThrow(() -> new ResourceNotFoundException("Goal","id", goalId));
    }
@RestController
公共级里程控制器{
@自动连线
私人贮油库贮油库;
@自动连线
私人目标存储目标存储;
@自动连线
私有状态存储库状态存储库;
@GetMapping(“/goals/{goalId}/milestones”)
公共页面getAllMilestonesByPostId(@PathVariable(value=“goalId”)长goalId,
可寻呼(可寻呼){
返回milestorepository.findByGoalId(goalId,可分页);
}
@后期映射(“/goals/{goalId}/milestones”)
公共里程碑createMilestone(@PathVariable(value=“goalId”)长goalId,
@有效@RequestBody里程碑(里程碑){
返回goalRepository.findById(goalId.map)(目标->{
//setStatus(statusRepository.findById(milestone.getStatus().getId());
里程碑。设定目标(goal);
返回milestorepository.save(里程碑);
}).orelsetrow(()->new ResourceNotFoundException(“目标”、“id”、“目标”);
}

,postman测试表明status为null,但它应该是status name

输入代码question@Antoniossss完成