Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1081891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:13:41+00:00 2026-05-16T22:13:41+00:00

I am new to HQL. This is a mysql Query. I need to convert

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:13:42+00:00Added an answer on May 16, 2026 at 10:13 pm

    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 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:

    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):

    @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.)


    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() 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In hibernate I want to run this JPQL / HQL query: select new org.test.userDTO(
I need to return a constant from an HQL query in NHIbernate SELECT new
I have following query written in HQL for Hibernate. ======================================================================== select new map(ret.retailerDesc as
I've got an HQL statement like this: select new map (f1 as field1, (select
New to PHP and MySQL, have heard amazing things about this website from Leo
New to Regex. I want to validate to this format: Any character allowed, except
I'd like to have a combined query for two persistent classes. In HQL this
I'm new user in StackOverflow, and I need a help in a HQL string..
I have a mapping that looks like this: <class name=Record> <map name=Values> <key column=RecordFK/>
I ran into a problem with Hibernate concerning hql query execution on classes which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.