超过两个实体的 Hibernate 复杂查询
Hibernate complex query for more then two entity
我正在尝试使用休眠查询来搜索和过滤我的实体,但我找不到任何解决方案。我有三个有界实体。
团体实体
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column
private String name;
@EqualsAndHashCode.Exclude
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(
name="students_groups",
joinColumns=@JoinColumn(name="group_id"),
inverseJoinColumns=@JoinColumn(name="student_id")
)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Student> students;
}
学生实体
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column(name="personal_number")
private int personalNumber;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column
private LocalDate birthday;
@Column
private String email;
@Enumerated(EnumType.STRING)
@Column(columnDefinition="enum('MALE','FEMALE')")
private Gender gender;
@Column
private String address;
@EqualsAndHashCode.Exclude
@OneToMany( cascade={CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(
name="students_courses",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="course_id")
)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Course> courses;
public Student(int id) {
this.id = id;
}
public String getGender() {
if(gender==null)
gender = Gender.valueOf("MALE");
return gender.toString();
}
public void setGender(String gender) {
if(gender==null)
gender = "MALE";
this.gender = Gender.valueOf(gender);
}
}
和课程实体
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column
private String name;
@Column
private String description;
}
我需要根据 group.id 和 course.id 找到分组 我尝试像这样进行查询
Query<Group> query = session.createQuery("from Group as g join g.students as s join s.courses as c where c.id like :courseId and g.id like :groupId", Group.class);
但它不起作用:(我将非常感谢你帮助我解决这个问题,并为我糟糕的英语感到抱歉:)
尝试以这种方式更正您的查询:
Query<Group> query = session.createQuery("select g from Group g join g.students s join s.courses c where c.id = :courseId and g.id = :groupId", Group.class);
请注意:
您只能将 like predicate 与 string_expression
一起使用。
TRUE
和 FALSE
值已弃用 @LazyCollection,因为您应该使用 @ElementCollection
的 JPA FetchType
属性, @OneToMany
, 或 @ManyToMany
集合。
我正在尝试使用休眠查询来搜索和过滤我的实体,但我找不到任何解决方案。我有三个有界实体。
团体实体
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column
private String name;
@EqualsAndHashCode.Exclude
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(
name="students_groups",
joinColumns=@JoinColumn(name="group_id"),
inverseJoinColumns=@JoinColumn(name="student_id")
)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Student> students;
}
学生实体
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column(name="personal_number")
private int personalNumber;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column
private LocalDate birthday;
@Column
private String email;
@Enumerated(EnumType.STRING)
@Column(columnDefinition="enum('MALE','FEMALE')")
private Gender gender;
@Column
private String address;
@EqualsAndHashCode.Exclude
@OneToMany( cascade={CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(
name="students_courses",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="course_id")
)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Course> courses;
public Student(int id) {
this.id = id;
}
public String getGender() {
if(gender==null)
gender = Gender.valueOf("MALE");
return gender.toString();
}
public void setGender(String gender) {
if(gender==null)
gender = "MALE";
this.gender = Gender.valueOf(gender);
}
}
和课程实体
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column
private String name;
@Column
private String description;
}
我需要根据 group.id 和 course.id 找到分组 我尝试像这样进行查询
Query<Group> query = session.createQuery("from Group as g join g.students as s join s.courses as c where c.id like :courseId and g.id like :groupId", Group.class);
但它不起作用:(我将非常感谢你帮助我解决这个问题,并为我糟糕的英语感到抱歉:)
尝试以这种方式更正您的查询:
Query<Group> query = session.createQuery("select g from Group g join g.students s join s.courses c where c.id = :courseId and g.id = :groupId", Group.class);
请注意:
您只能将 like predicate 与
string_expression
一起使用。TRUE
和FALSE
值已弃用 @LazyCollection,因为您应该使用@ElementCollection
的 JPAFetchType
属性,@OneToMany
, 或@ManyToMany
集合。