枚举参数不会从数据库返回值

时间:2019-05-22 03:39:35

标签: sql hibernate spring-boot jpa enums

我的项目中有一些需要ENUM的数据,所以我决定实现它。问题是我没有从数据库取回数据。必须缺少一些配置,我无法弄清楚。这是一些代码示例

存储库:

 @Query(value="SELECT student_account_table.* FROM student_account student_account_table INNER JOIN student student_table ON student_account_table.student_account_id = studdent_table.student.id WHERE student_table.grade= :studentGrade", nativeQuery=true)
 List<StudentAccount> getStudentAccounts(@Param("studentGrade") StudentGrades studentGrade);

StudentAccounImplementing

@Override
    public List<StudentAccount> getStudentAccounts(StudentGrades studentGrade) {
        return studentAccountRepository.getStudentAccounts(studentGrade);
    }

调用方法:

    public void eraseStudent(){
            List<StudentAccount> eraseThis= studentAccountImplementation.getStudentAccounts(StudentGrades.E);    

 //This method doesn't work because the list is empty        
            studentAccountImplementation.eraseStudents(eraseThis);
        }

枚举:

public enum StudentGrades{
    A, B, C, D, E;
}

实体

@Column(name = "student_grade")
    @Enumerated(EnumType.STRING)
    private StudentGrades studentGrade;

为简洁起见,缺少构造函数和其他代码。 我尝试了在SO和强大的Google上找到的所有可能的解决方案,但均未成功。谁能帮我?预先感谢。

1 个答案:

答案 0 :(得分:0)

通过将Enum值转换为字符串找到了解决方案。就是这样

修复:

 public void eraseStudent(){
            List<StudentAccount> eraseThis= studentAccountImplementation.getStudentAccounts(StudentGrades.E.toString());    

 //This method doesn't work because the list is empty        
            studentAccountImplementation.eraseStudents(eraseThis);
        }

我希望答案能对某人有所帮助。