如何编写 HQL 查询?
How to write a query for HQL?
我有一个项目:监控学生的成绩。
我有一些实体。有:
@Entity
@Table(name = "clazzes")
public class Clazz extends BaseEntity{
@NotEmpty
@Column(name = "number")
private String number;
@NotEmpty
@Column(name = "letter")
private String letter;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "clazz", fetch = FetchType.EAGER)
private Set<Student> students;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="school_id")
private School school;
// getters and setters
}
@Entity
@Table(name = "students")
public class Student extends Person {
@JsonIgnore
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "parents_students", joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "parent_id"))
private List<Parent> parents;
@NotNull
@JsonIgnore
@ManyToOne
@JoinColumn(name = "clazz_id")
private Clazz clazz;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student", fetch = FetchType.EAGER)
private Set<Grade> grades;
// getters and setters
}
@Entity
@Table(name = "grades")
public class Grade extends BaseEntity{
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "shedule_id")
private Shedule shedule;
@NotNull
@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="student_id")
private Student student;
@Column(name = "task")
private String task;
@Column(name = "mark")
private Integer mark;
// getters and setters
}
@Entity
@Table(name = "shedule")
public class Shedule extends BaseEntity implements Comparable<Shedule>{
@NotNull
@Column(name = "date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
@DateTimeFormat(pattern = "dd.MM.yyyy")
private Date date;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="period_id")
private Period period;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="subject_id")
private Subject subject;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="clazz_id")
private Clazz clazz;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="teacher_id")
private Teacher teacher;
@Column(name = "job")
private String job;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "shedule", fetch = FetchType.EAGER)
private Set<Grade> grades;
// getters and setters
}
请告诉我如何获取HQL查询以下数据:
日程表、学生、年级。
我正在尝试编写这样的查询:
Query query = this.em.createQuery("SELECT DISTINCT shedule, students, grade " +
"FROM Shedule shedule " +
"INNER JOIN shedule.clazz clazz " +
"INNER JOIN clazz.students students " +
"LEFT JOIN students.grades grade with (grade.shedule.id = shedule.id)");
但是由于额外的条件 "with (grade.shedule.id = shedule.id)":
我得到了以下错误
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause referenced two different from-clause elements [SELECT DISTINCT shedule, students, grade FROM com.vizaco.onlinecontrol.model.Shedule shedule INNER JOIN shedule.clazz clazz INNER JOIN clazz.students students LEFT JOIN students.grades grade with (grade.shedule.id = shedule.id)
如果项目采用这种架构,是否可以编写HQL查询?或者是否有必要改变项目的架构?还是必须写native query?
SELECT DISTINCT shedule, students, grade
FROM Shedule shedule
INNER JOIN shedule.clazz clazz
INNER JOIN clazz.students students
LEFT JOIN students.grades grade
WHERE grade IS NULL OR grade.shedule.id = shedule.id
我有一个项目:监控学生的成绩。 我有一些实体。有:
@Entity
@Table(name = "clazzes")
public class Clazz extends BaseEntity{
@NotEmpty
@Column(name = "number")
private String number;
@NotEmpty
@Column(name = "letter")
private String letter;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "clazz", fetch = FetchType.EAGER)
private Set<Student> students;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="school_id")
private School school;
// getters and setters
}
@Entity
@Table(name = "students")
public class Student extends Person {
@JsonIgnore
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "parents_students", joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "parent_id"))
private List<Parent> parents;
@NotNull
@JsonIgnore
@ManyToOne
@JoinColumn(name = "clazz_id")
private Clazz clazz;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student", fetch = FetchType.EAGER)
private Set<Grade> grades;
// getters and setters
}
@Entity
@Table(name = "grades")
public class Grade extends BaseEntity{
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "shedule_id")
private Shedule shedule;
@NotNull
@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="student_id")
private Student student;
@Column(name = "task")
private String task;
@Column(name = "mark")
private Integer mark;
// getters and setters
}
@Entity
@Table(name = "shedule")
public class Shedule extends BaseEntity implements Comparable<Shedule>{
@NotNull
@Column(name = "date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
@DateTimeFormat(pattern = "dd.MM.yyyy")
private Date date;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="period_id")
private Period period;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="subject_id")
private Subject subject;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="clazz_id")
private Clazz clazz;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="teacher_id")
private Teacher teacher;
@Column(name = "job")
private String job;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "shedule", fetch = FetchType.EAGER)
private Set<Grade> grades;
// getters and setters
}
请告诉我如何获取HQL查询以下数据: 日程表、学生、年级。
我正在尝试编写这样的查询:
Query query = this.em.createQuery("SELECT DISTINCT shedule, students, grade " +
"FROM Shedule shedule " +
"INNER JOIN shedule.clazz clazz " +
"INNER JOIN clazz.students students " +
"LEFT JOIN students.grades grade with (grade.shedule.id = shedule.id)");
但是由于额外的条件 "with (grade.shedule.id = shedule.id)":
我得到了以下错误java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause referenced two different from-clause elements [SELECT DISTINCT shedule, students, grade FROM com.vizaco.onlinecontrol.model.Shedule shedule INNER JOIN shedule.clazz clazz INNER JOIN clazz.students students LEFT JOIN students.grades grade with (grade.shedule.id = shedule.id)
如果项目采用这种架构,是否可以编写HQL查询?或者是否有必要改变项目的架构?还是必须写native query?
SELECT DISTINCT shedule, students, grade
FROM Shedule shedule
INNER JOIN shedule.clazz clazz
INNER JOIN clazz.students students
LEFT JOIN students.grades grade
WHERE grade IS NULL OR grade.shedule.id = shedule.id