我有两个实体类。
1.Course.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
2.Student.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToMany
private List<Course> course;
现在我需要一个类(class) id = 1 或类(class) id 为空的学生列表; 查询如“FROM Student st WHERE st.course = 1 or course id is null”。 如何在hql中编写这个查询?
最佳答案
您需要加入:
select s from Student s inner join s.courses c where c.id = 1
请注意,我选择在此查询中将字段 course
重命名为 courses
:学生有很多类(class),最后的 s 使得很清楚。
关于java - hibernate 查询使用列表属性检查单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009731/