繁体   English   中英

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 数据库结构如下所示:

H2数据库结构

我正在尝试通过 rest controller向学生添加任务(rest Z594C18AB5F2C6E0403 工作正常)

@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'}]

如您所见,Task 似乎确实被添加到了数据库中——除了它没有:(

数据库数据

☝️ 应该有一个“4, 3”(4 是任务 ID,3 是学生“编号”)。

哦,当我这样做时,这是我的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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM