如何为HQL编写查询?

时间:2015-07-19 14:40:33

标签: hibernate 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查询以下数据: Shedule,学生,年级。

我试图写这样的查询:

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)");

但由于额外的条件而导致以下错误&#34; with(grade.shedule.id = shedule.id)&#34;:

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查询?或者是否有必要改变项目的架构?或者是否有必要编写本机查询?

1 个答案:

答案 0 :(得分:1)

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