我有一个问题,我不知道如何在 Hibernate 中创建关系数据库以及如何创建 json i post 方法。
我想通过现有的老师和新的学生打交道,我想找到所有有特定老师的学生。我想在学生表中为老师创建类似外键的东西。
我的代码:
基础实体
@MappedSuperclass
@Data
public class BaseEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
学生
@Entity
@Table(name = "student")
public class Student extends BaseEntity{
@Column(name = "name")
private String name;
@ManyToOne
private Teacher teacher;
...
}
老师
@Entity
@Table(name = "teacher")
public class Teacher extends BaseEntity{
@Column(name = "name")
private String name;
...
}
教师控制器
@RequestMapping(value = "/add", method = RequestMethod.POST)
public void addTeacher(@RequestBody Teacher teacher) {
teacherService.addTeacher(teacher);
}
学生控制器
@RequestMapping(value = "/add", method = RequestMethod.POST)
public void addStudent(@RequestBody Student student) {
studentService.addStudent(student);
}
我创建老师时的 JSON 身体:
{
"name" : "Test"
}
学生发布方法应该是这样的吗?
{
"name": "TestStudent",
"teacher": {
"id": 1,
"description": "Test"
}
}
翻过高山走不出你
相关分类