简体   繁体   English

如何将MySQL查询转换为HQL查询?

[英]How to convert MySQL query into HQL query?

I am new to HQL. 我是HQL的新手。 This is a mysql Query. 这是一个mysql查询。 I need to convert it into HQL query. 我需要将其转换为HQL查询。 How to do that any suggestions please? 请问该怎么做?

    `SELECT STUDENT.ID, STUDENT.NAME, STUDENT.GRADE_ID, STUDENT.CLASS, GRADE.NAME FROM
     STUDENT INNER JOIN GRADE ON STUDENT.GRADE_ID = GRADE.GRADE_ID`

STUDENT[ID, NAME, GRADE_ID, CLASS] 学生[ID,NAME,GRADE_ID,CLASS]

GRADE[GRADE_ID, GRADE_NAME] GRADE [GRADE_ID,GRADE_NAME]

The resultset of the above query will be something like ID, NAME, GRADE_NAME, CLASS. 上述查询的结果集将类似于ID,NAME,GRADE_NAME,CLASS。


Final Update: 最终更新:

I have 2 tables STUDENT[ID, NAME, GRADE_ID, CLASS] GRADE[GRADE_ID, GRADE_NAME] 我有2个表STUDENT[ID, NAME, GRADE_ID, CLASS] GRADE[GRADE_ID, GRADE_NAME]

With the SQL query SELECT STUDENT.ID, STUDENT.NAME, STUDENT.GRADE_ID, STUDENT.CLASS, GRADE.NAME FROM STUDENT INNER JOIN GRADE ON STUDENT.GRADE_ID = GRADE.GRADE_ID I used to show a New table STUDENT[ID, NAME, GRADE_NAME, CLASS] was working in SQL. 使用SQL查询SELECT STUDENT.ID, STUDENT.NAME, STUDENT.GRADE_ID, STUDENT.CLASS, GRADE.NAME FROM STUDENT INNER JOIN GRADE ON STUDENT.GRADE_ID = GRADE.GRADE_ID我曾用来显示新表STUDENT[ID, NAME, GRADE_NAME, CLASS]正在使用SQL。 For this function there is no input, return type is a list(all the records of the result table) 这个函数没有输入,返回类型是一个列表(结果表的所有记录)

This is what i want to do in HQL or JPQL , How to get the List of Objects where, Properties of the Object were id, name, gradename, class. 这是我想在HQL or JPQL ,如何获取对象列表,其中对象的属性为id,名称,等级名称,类。

How to do it with HQL. 如何使用HQL做到这一点。

You wouldn't do that in Hibernate. 您不会在Hibernate中这样做。 The whole point of using hibernate is that you are dealing with objects. 使用休眠的全部目的是要处理对象。

So I guess your Student class would have a List of type Grade 所以我想您的学生班级会有一个成绩类型列表

@Entity
public class Student{
    // accessors and id omitted
    @OneToMany(mappedBy="student")
    private List<Grade> grades;
}

@Entity
public class Grade{
    // accessors and id omitted
    @ManyToOne
    private Student student;
}

You would look up the grade via session.get() and then do grade.getStudent() to access the student: 您可以通过session.get()查找成绩,然后执行grade.getStudent()来访问学生:

Grade grade = (Grade) session.get(Grade.class, gradeId);
Student student = grade.getStudent();

HQL queries are designed for Scenarios that are too complex for these lookup methods. HQL查询设计用于对于这些查找方法而言过于复杂的方案。

Edit: I just realized that the question is tagged jpa . 编辑:我刚刚意识到问题被标记为jpa Then of course you would not be using HQL, but JPQL instead. 那么,您当然不会使用HQL,而将使用JPQL。 And also you'd be using this code: 而且您还将使用以下代码:

Grade grade = entityManager.find(Grade.class, gradeId); // no cast needed with JPA 2
Student student = grade.getStudent();

Edit: given the requirements you added in your comments I'd change the data model to something like this (ids omitted): 编辑:给定您在注释中添加的要求,我会将数据模型更改为以下形式(省略了ID):

@Entity
public class Student{
    public List<Course> getCourses(){
        return courses;
    }
    public void setCourses(List<Course> courses){
        this.courses = courses;
    }
    @OneToMany(mappedBy="student")
    private List<Course> courses;
}

@Entity
public class Course{
    @ManyToOne(optional=false)
    private Student student;
    @ManyToOne(optional=false)
    private Grade grade;
    public void setStudent(Student student){
        this.student = student;
    }
    public Student getStudent(){
        return student;
    }
    public Grade getGrade(){
        return grade;
    }
    public void setGrade(Grade grade){
        this.grade = grade;
    }
}

@Entity
public class Grade{
    @OneToMany(mappedBy="grade")
    private Set<Course> courses;
    public void setCourses(Set<Course> courses){
        this.courses = courses;
    }
    public Set<Course> getCourses(){
        return courses;
    }
}

And I'd query like this: 我想这样查询:

Grade grade = entityManager.find(Grade.class, 1L);
List<Student> studentsWithThisGrade = new ArrayList<Student>();
for(Course course : grade.getCourses()){
    studentsWithThisGrade.add(course.getStudent());
}

(But Grade should probably not be an entity, but either a numeric value or an enum.) (但Grade可能不应该是一个实体,而应该是一个数值或一个枚举。)


It turns out that the OP uses plain hibernate after all, no JPA. 事实证明,OP毕竟使用普通休眠,而不使用JPA。 So whenever you see entityManager.find() above, replace that in your mind with session.get() :-) 因此,每当您看到上面的entityManager.find()entityManager.find()替换为session.get() :-)

Try this. 尝试这个。

public List<Student> getByGradeId(EntityManager entityManager, Grade grade) {
   Query query = entityManager.createQuery("from Student where grade=:grade");
   query.setParameter("grade", grade);
   return (List<Student>) query.getResultList();
}

You'll need a try/catch in there too. 您也需要在那里尝试/捕捉。

Read section 3.4 of this ! 阅读本节3.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM