Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa Spring boot Enity无法添加多个实体_Jpa_Spring Boot_Crud_Spring Data Rest - Fatal编程技术网

Jpa Spring boot Enity无法添加多个实体

Jpa Spring boot Enity无法添加多个实体,jpa,spring-boot,crud,spring-data-rest,Jpa,Spring Boot,Crud,Spring Data Rest,我的学习项目中有这些实体,我无法理解为什么我不能添加员工: @Entity @Table(name = "department") @Data public class Department { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "department_id") @JsonProperty("department_id") private Long dep

我的学习项目中有这些实体,我无法理解为什么我不能添加员工:

@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,我使用
http://localhost:9096/employee
Spring数据生成的端点使用以下JSON主体:

{
    "employee_name": "Tim",
    "office_id" : 1,
    "department": {
        "department_id": 1
    }
}
我总是会遇到一个例外:“部门id”列不能为空”

解决这个问题的正确方法应该是什么


可以在

上找到完整的项目。您的数据库中似乎不存在部门id,或者您的多通配置不正确。所以它导致了错误。您可以参考下面的多人关系

@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;
    }
}
@实体
@表(name=“course”)
公共类课程实现可序列化{
私有int-id;
私立学校学生;
公共课程(){
}
@OneToMany(mappedBy=“course”,cascade=CascadeType.ALL)
公共集getStudents(){
留学生;
}
}
@实体
公共类学生实现可序列化{
私有int-id;
私人课程;
公立学生(){
}
@许多酮
@JoinColumn(name=“课程id”)
公共课程{
返回路线;
}
}

您可以在帖子中查看工作示例,该示例似乎数据库中不存在部门id,或者您的多通配置不正确。所以它导致了错误。您可以参考下面的多人关系

@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;
    }
}
@实体
@表(name=“course”)
公共类课程实现可序列化{
私有int-id;
私立学校学生;
公共课程(){
}
@OneToMany(mappedBy=“course”,cascade=CascadeType.ALL)
公共集getStudents(){
留学生;
}
}
@实体
公共类学生实现可序列化{
私有int-id;
私人课程;
公立学生(){
}
@许多酮
@JoinColumn(name=“课程id”)
公共课程{
返回路线;
}
}

您可以在帖子中查看工作示例

我不确定Spring Data REST是否支持这样嵌入部门(通过使用
{“department\u id”:1}

{
    "employee_name": "Tim",
    "office_id" : 1,
    "department": "http://localhost:9096/departments/1"
}

这将创建一个链接到ID为1的部门的新员工。如果要将该员工链接到一个新部门,可能必须首先创建该部门,并以相同的方式将其链接到该员工。

我不确定Spring Data REST是否支持像这样嵌入该部门(使用
{“department\u ID:1}
。默认情况下,如果要将资源(如部门)链接到另一个资源(如员工),请使用资源链接,例如:

{
    "employee_name": "Tim",
    "office_id" : 1,
    "department": "http://localhost:9096/departments/1"
}

这将创建一个链接到ID为1的部门的新员工。如果要将该员工链接到一个新部门,可能必须先创建该部门,然后以相同的方式将其链接到该员工。

数据库中确实存在“部门ID”。请参阅参考资料/schema-mysql.sqlNo,我的意思是它的值不是ccolumn name.“department\u id”在数据库中不存在。请参阅参考资料/schema-mysql.sqlNo,我指的是它的值而不是列名。