Spring 启动 CrudRepository 没有将实体保存到我的数据库
Spring Boot CrudRepository not saving entities to my database
我已经处理了几个小时了,但似乎找不到问题所在。
关于一些上下文,这是我的数据库模式:
这是我的 Student
class :
@Entity
@NoArgsConstructor
@Data
public class Student {
@NotNull
@Id
private long number;
@NotBlank
@NotNull
private String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "students", cascade = CascadeType.ALL)
@JsonIgnore
private List<Task> tasks;
}
这是我的 Task
class :
public class Task {
@NotNull
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
@NotNull
private String description;
@ManyToMany
@JoinTable(
name = "student_tasks",
joinColumns = @JoinColumn(name = "tasks_id"),
inverseJoinColumns = @JoinColumn(name = "student_number")
)
@JsonIgnore
private List<Student> students;
}
因此,我的 H2 数据库结构如下所示:
我正在尝试通过休息控制器向学生添加任务(休息控制器工作正常):
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api")
public class MyRestController {
@Autowired
private Exercises exercises;
[...]
@PostMapping(value = "/students/student/{studentId}/complete/{taskId}")
public void completeTask(
@PathVariable(value = "studentId") long studentId,
@PathVariable(value = "taskId") long taskId
) {
exercises.completeTask(studentId, taskId);
}
}
这是我的 Exercises
class :
@Service
@Slf4j
public class Exercises {
@Autowired
private StudentDB studentDB;
@Autowired
private TaskDB taskDB;
[...]
public void completeTask(long studentId, long taskId) {
var student = studentDB.findById(studentId).orElse(null);
var task = taskDB.findById(taskId).orElse(null);
log.info(student.toString());
log.info(task.toString());
log.info(student.getTasks().toString());
student.getTasks().add(task);
studentDB.save(student);
log.info(studentDB.findById(studentId).orElse(null).getTasks().toString());
}
}
这是代码中的日志:
2022-01-04 14:29:42.827 INFO : Student{number=3, name='StudentC'}
2022-01-04 14:29:42.828 INFO : Task{id=4, description='Exercice 4'}
2022-01-04 14:18:54.180 INFO : [Task{id=1, description='Exercice 1'}, Task{id=2, description='Exercice 2'}, Task{id=3, description='Exercice 3'}]
2022-01-04 14:18:54.189 INFO : [Task{id=1, description='Exercice 1'}, Task{id=2, description='Exercice 2'}, Task{id=3, description='Exercice 3'}, Task{id=4, description='Exercice 4'}]
如您所见,任务似乎确实已添加到数据库中 - 除了它没有 :(
☝️ There should be a "4, 3" (4 being the task id and 3 the student "number").
哦,当我在做的时候,这是我的 StudentDB
class :
public interface StudentDB extends CrudRepository<Student, Long> {}
我对 Spring 还是很陌生,所以可能只是我遗漏了一些东西:/
预先感谢您的帮助!
正如@MauricePerry 评论的那样,将学生添加到任务中而不是将任务添加到学生中
对于遇到同样问题的任何人,我现在有以下代码:
@Service
@Slf4j
public class Exercises {
@Autowired
private StudentDB studentDB;
@Autowired
private TaskDB taskDB;
public void completeTask(long studentId, long taskId) {
var student = studentDB.findById(studentId).orElse(null);
var task = taskDB.findById(taskId).orElse(null);
task.getStudents().add(student);
taskDB.save(task);
}
}
我已经处理了几个小时了,但似乎找不到问题所在。
关于一些上下文,这是我的数据库模式:
这是我的 Student
class :
@Entity
@NoArgsConstructor
@Data
public class Student {
@NotNull
@Id
private long number;
@NotBlank
@NotNull
private String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "students", cascade = CascadeType.ALL)
@JsonIgnore
private List<Task> tasks;
}
这是我的 Task
class :
public class Task {
@NotNull
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
@NotNull
private String description;
@ManyToMany
@JoinTable(
name = "student_tasks",
joinColumns = @JoinColumn(name = "tasks_id"),
inverseJoinColumns = @JoinColumn(name = "student_number")
)
@JsonIgnore
private List<Student> students;
}
因此,我的 H2 数据库结构如下所示:
我正在尝试通过休息控制器向学生添加任务(休息控制器工作正常):
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api")
public class MyRestController {
@Autowired
private Exercises exercises;
[...]
@PostMapping(value = "/students/student/{studentId}/complete/{taskId}")
public void completeTask(
@PathVariable(value = "studentId") long studentId,
@PathVariable(value = "taskId") long taskId
) {
exercises.completeTask(studentId, taskId);
}
}
这是我的 Exercises
class :
@Service
@Slf4j
public class Exercises {
@Autowired
private StudentDB studentDB;
@Autowired
private TaskDB taskDB;
[...]
public void completeTask(long studentId, long taskId) {
var student = studentDB.findById(studentId).orElse(null);
var task = taskDB.findById(taskId).orElse(null);
log.info(student.toString());
log.info(task.toString());
log.info(student.getTasks().toString());
student.getTasks().add(task);
studentDB.save(student);
log.info(studentDB.findById(studentId).orElse(null).getTasks().toString());
}
}
这是代码中的日志:
2022-01-04 14:29:42.827 INFO : Student{number=3, name='StudentC'}
2022-01-04 14:29:42.828 INFO : Task{id=4, description='Exercice 4'}
2022-01-04 14:18:54.180 INFO : [Task{id=1, description='Exercice 1'}, Task{id=2, description='Exercice 2'}, Task{id=3, description='Exercice 3'}]
2022-01-04 14:18:54.189 INFO : [Task{id=1, description='Exercice 1'}, Task{id=2, description='Exercice 2'}, Task{id=3, description='Exercice 3'}, Task{id=4, description='Exercice 4'}]
如您所见,任务似乎确实已添加到数据库中 - 除了它没有 :(
☝️ There should be a "4, 3" (4 being the task id and 3 the student "number").
哦,当我在做的时候,这是我的 StudentDB
class :
public interface StudentDB extends CrudRepository<Student, Long> {}
我对 Spring 还是很陌生,所以可能只是我遗漏了一些东西:/
预先感谢您的帮助!
正如@MauricePerry 评论的那样,将学生添加到任务中而不是将任务添加到学生中
对于遇到同样问题的任何人,我现在有以下代码:
@Service
@Slf4j
public class Exercises {
@Autowired
private StudentDB studentDB;
@Autowired
private TaskDB taskDB;
public void completeTask(long studentId, long taskId) {
var student = studentDB.findById(studentId).orElse(null);
var task = taskDB.findById(taskId).orElse(null);
task.getStudents().add(student);
taskDB.save(task);
}
}