【发布时间】:2019-01-24 13:09:19
【问题描述】:
我正在使用 Spring Boot(版本 - 2.1.1)。我通过rest api为CRUD操作公开了一个一对多的数据库模型。该模型如下所示。如何配置 POST /departments api(创建部门对象)以仅接受输入 json 正文中的组织 ID?
@PostMapping
public Long createDepartment(@RequestBody Department Department) {
Department d = departmentService.save(Department);
return d.getId();
}
注意 - 我不想在创建部门时允许创建组织对象。
模型对象映射
@Entity
@Table(name="ORGANIZATIONS")
public class Organization{
@Id
@GeneratedValue
Private long id;
@Column(unique=true)
Private String name;
@OneToMany(mappedBy = "organization", fetch = FetchType.EAGER)
private List<Department> departments;
}
@Entity
@Table(name="DEPARTMENTS")
Public class Department{
@Id
@GeneratedValue
Private long id;
@Column(unique=true)
Private String name;
@ManyToOne(fetch = FetchType.EAGER)
private Organization organization;
}
谢谢!
【问题讨论】:
标签: spring-boot spring-data-jpa spring-rest