OneToMany(双向)在保存时不在子表中插入外键

时间:2018-04-15 13:29:10

标签: jpa spring-data-jpa

外键未保存到任务表中,在通过其余api保存持有者时没有形成关联。 请让我知道我在这里缺少什么

REST API URL:localhost:8080 / priority / holder

PAYLOAD: {   "姓名":" Madhu",   "任务":[     {       " name":"更新",       "输入":"技术"     },     {       " name":" jogg",       "输入":"健身"     },     {       "姓名":"工作",       "输入":"项目"     }   ] }

我在这里添加我的控制器,实体和服务图像

[控制器] [1]

@RestController
@RequestMapping(value = "/priority")
public class TaskController {
    @Autowired
    TaskService taskService;
    @PostMapping(value = "/holder")
    public Holder createHolder(@RequestBody Holder holder) {
        return taskService.createHolder(holder);
    }
}

@Service
public class TaskService {
    @Autowired
    TaskRepositriy taskRepo;
    @Autowired
    HolderRepositry holderRepo;
    public Holder createHolder(Holder holder) {
        return holderRepo.save(holder);
    }

}

@Entity
public class Holder {
    @Id
    @GeneratedValue
    private long id;
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "holder",fetch=FetchType.LAZY)
    private List<Task> task;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Task> getTask() {
        return task;
    }
    public void setTask(List<Task> task) {
        this.task = task;
    }   
}

@Entity
public class Task {
    @Id
    @GeneratedValue
    private long id;
    private String name;
    @ManyToOne
    @JoinColumn(name="holder_task")
    private Holder holder;
    public Holder getHolder() {
        return holder;
    }
    public void setHolder(Holder holder) {
        this.holder = holder;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

0 个答案:

没有答案