联接查询可从mysql运行,但不能从java

时间:2019-05-20 20:05:31

标签: sql spring-boot

我的应用程序中有一个联接查询。它应该检索尚未创建学生帐户的学生。我认为该查询在我编写单元测试之前一直有效,并且我注意到即使该帐户已经创建,该查询仍返回相同的学生。

查询

SELECT student_table.* FROM student student_table WHERE NOT EXISTS (SELECT app_relationship_table.student_id FROM relationship_table app_relationship_table WHERE student_table.student_id = app_relationship_table.student_id)

以上查询的意思是:给所有存在于students表中但不存在于app_relationship_table中的学生,这样我应该知道哪些学生丢失了他们的帐户。 有趣的是,此查询在MySql Workbench中可以正常工作,但在我的Java代码中不起作用。它一直给我刚开始没有学生帐户的学生,因此在数据库中创建重复的数据。

我从本地主机运行该项目。当我第一次运行它时,它可以正常工作,但是当我重新加载页面或执行预定的任务时,即使创建了帐户并用新的外键更新了关系表,它也会弄乱并给我相同的学生。我正在使用CrudRepository保留数据

创建学生帐户代码:

@Override
    public void createStudentAccount(List<Student> studentsWithoutStudentAccount) {

        studentsWithoutStudentAccount.forEach(student->{

            String username = "username";
            String password = "password";

            StudentAccount studentAccount = new StudentAccount (username, password, student);
            save(studentAccount);

        });

    }

有人可以帮助我吗?我不知道我在这里想念什么

0 个答案:

没有答案