如何将MySQL查询转换为HQL查询? [英] How to convert MySQL query into HQL query?

查看:211
本文介绍了如何将MySQL查询转换为HQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是HQL的新手.这是一个mysql查询.我需要将其转换为HQL查询.请问该怎么做?

I am new to HQL. This is a mysql Query. I need to convert it into HQL query. 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`

学生[ID,NAME,GRADE_ID,CLASS]

GRADE [GRADE_ID,GRADE_NAME]

上述查询的结果集将类似于ID,NAME,GRADE_NAME,CLASS.

最终更新:

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

使用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中工作. 该函数没有输入,返回类型是一个列表(结果表的所有记录)

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. For this function there is no input, return type is a list(all the records of the result table)

这是我想在HQL or JPQL中做的事情,如何获取对象列表,其中,对象的属性为id,名称,等级名称,类.

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.

推荐答案

您不会在Hibernate中做到这一点.使用休眠的全部目的是要处理对象.

You wouldn't do that in 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:

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

HQL查询是针对对于这些查找方法而言过于复杂的情况而设计的.

HQL queries are designed for Scenarios that are too complex for these lookup methods.

我刚刚意识到问题被标记为jpa.那么,您当然不会使用HQL,而将使用JPQL.而且您还将使用以下代码:

I just realized that the question is tagged jpa. Then of course you would not be using HQL, but JPQL instead. 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();


鉴于您在注释中添加的要求,我将数据模型更改为以下形式(省略了ID):


given the requirements you added in your comments I'd change the data model to something like this (ids omitted):

@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;
    }
}

我会这样查询:

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

(但Grade可能不应该是一个实体,而应该是一个数值或一个枚举.)

(But Grade should probably not be an entity, but either a numeric value or an enum.)

事实证明,OP毕竟使用普通休眠,而不使用JPA.因此,每当您看到上面的entityManager.find()时,就将其替换为session.get():-)

It turns out that the OP uses plain hibernate after all, no JPA. So whenever you see entityManager.find() above, replace that in your mind with session.get() :-)

这篇关于如何将MySQL查询转换为HQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆