Spring 启动实体无法使用 ManyToOne 添加实体
Spring boot Enity failed to add entity with ManyToOne
我的学习项目中有这些实体,但我不明白为什么我无法添加员工:
@Entity
@Table(name = "department")
@Data
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "department_id")
@JsonProperty("department_id")
private Long department_id;
@Column(name = "department_name")
@JsonProperty("department_name")
private String department_name;
}
@Entity
@Table(name = "office")
@Data
public class Office {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "office_id")
@JsonProperty("office_id")
private Long office_id;
@Column(name = "office_name")
@JsonProperty("office_name")
private String office_name;
}
@Entity
@Table(name = "employee")
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "employee_id")
@JsonProperty("employee_id")
private Long employee_id;
@Column(name = "employee_name")
@JsonProperty("employee_name")
private String employee_name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "department_id")
private Department department;
private Long office_id;
}
使用 Postman,我发送 POST 以使用 Spring 数据 REST 生成的 http://localhost:9096/employee
端点在部门 'engineer' 中添加一名员工 [=27] =]正文:
{
"employee_name": "Tim",
"office_id" : 1,
"department": {
"department_id": 1
}
}
我总是遇到异常:"Column 'department_id' cannot be null"。
解决这个问题的正确方法应该是什么?
找到
似乎 department_id 在您的数据库中不存在,或者您的 ManyToOne 配置不正确。所以它会导致错误。可以参考下面这个ManyToOne关系
@Entity
@Table(name = "course")
public class Course implements Serializable{
private int id;
private Set<Student> students;
public Course(){
}
@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
public Set<Student> getStudents() {
return students;
}
}
@Entity
public class Student implements Serializable{
private int id;
private Course course;
public Student() {
}
@ManyToOne
@JoinColumn(name = "course_id")
public Course getCourse() {
return course;
}
}
查看工作示例
我不确定 Spring Data REST 是否支持像那样嵌入部门(通过使用 { "department_id": 1 }
。默认情况下,如果你想 link 资源(例如。 a department) to another (eg. an employee), 你使用资源link,例如:
{
"employee_name": "Tim",
"office_id" : 1,
"department": "http://localhost:9096/departments/1"
}
这将创建一个新员工 linked 到 ID 为 1 的部门。如果你想 link 员工到一个新部门,你可能必须先创建部门, link 以同样的方式发送给员工。
我的学习项目中有这些实体,但我不明白为什么我无法添加员工:
@Entity
@Table(name = "department")
@Data
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "department_id")
@JsonProperty("department_id")
private Long department_id;
@Column(name = "department_name")
@JsonProperty("department_name")
private String department_name;
}
@Entity
@Table(name = "office")
@Data
public class Office {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "office_id")
@JsonProperty("office_id")
private Long office_id;
@Column(name = "office_name")
@JsonProperty("office_name")
private String office_name;
}
@Entity
@Table(name = "employee")
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "employee_id")
@JsonProperty("employee_id")
private Long employee_id;
@Column(name = "employee_name")
@JsonProperty("employee_name")
private String employee_name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "department_id")
private Department department;
private Long office_id;
}
使用 Postman,我发送 POST 以使用 Spring 数据 REST 生成的 http://localhost:9096/employee
端点在部门 'engineer' 中添加一名员工 [=27] =]正文:
{
"employee_name": "Tim",
"office_id" : 1,
"department": {
"department_id": 1
}
}
我总是遇到异常:"Column 'department_id' cannot be null"。
解决这个问题的正确方法应该是什么?
找到似乎 department_id 在您的数据库中不存在,或者您的 ManyToOne 配置不正确。所以它会导致错误。可以参考下面这个ManyToOne关系
@Entity
@Table(name = "course")
public class Course implements Serializable{
private int id;
private Set<Student> students;
public Course(){
}
@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
public Set<Student> getStudents() {
return students;
}
}
@Entity
public class Student implements Serializable{
private int id;
private Course course;
public Student() {
}
@ManyToOne
@JoinColumn(name = "course_id")
public Course getCourse() {
return course;
}
}
查看工作示例
我不确定 Spring Data REST 是否支持像那样嵌入部门(通过使用 { "department_id": 1 }
。默认情况下,如果你想 link 资源(例如。 a department) to another (eg. an employee), 你使用资源link,例如:
{
"employee_name": "Tim",
"office_id" : 1,
"department": "http://localhost:9096/departments/1"
}
这将创建一个新员工 linked 到 ID 为 1 的部门。如果你想 link 员工到一个新部门,你可能必须先创建部门, link 以同样的方式发送给员工。