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`
STUDENT[ID, NAME, GRADE_ID, CLASS]
GRADE[GRADE_ID, GRADE_NAME]
The resultset of the above query will be something like ID, NAME, GRADE_NAME, CLASS.
Final Update:
I have 2 tables 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.
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.
How to do it with HQL.
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
You would look up the grade via session.get() and then do
grade.getStudent()to access the student:HQL queries are designed for Scenarios that are too complex for these lookup methods.
Edit: 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:Edit: given the requirements you added in your comments I’d change the data model to something like this (ids omitted):
And I’d query like this:
(But Grade should probably not be an entity, but either a numeric value or an enum.)
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 withsession.get()🙂