HQL Query Objects in Hibernate
In our previous hibernate article, we have seen CRUD Operations. Similar to SQL Queries, Hibernate uses a Language called HQL – Hibernate Query Language. This is almost similar to SQL, But here instead of Table name we use Entity name and instead of Column name we use Entity properties name.

Once we are through this code, we can understand HQL.

Lets see an example code of using Query Objects in Hibernate. For the purpose of example, I have created a Table with name Student_Table and populated with some values.

Table 1

In this below example, we are going to use Query Object and HQL to list sname from above table.

Structure:
Structure

Student.java

package com.javainfinite.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Student_Table")
public class Student {
    
    @Id
    @GeneratedValue
    private int sid;
    private String sname;
    private String sdept;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }
    
}

NewHibernateUtil.java

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

hibernate.cfg.xml

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">URL</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.show_sql">true</property>
    <mapping class="com.javainfinite.pojo.Student"/>
  </session-factory>
</hibernate-configuration>

Execution.java

package com.javainfinite.execute;

import com.javainfinite.pojo.NewHibernateUtil;
import com.javainfinite.pojo.Student;
import java.util.List;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class Execution {
    
    public static void main(String args[])
    {
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        try
        {
            Transaction trans=session.beginTransaction();
            Query query=session.createQuery("from Student"); //Similar to Select * from Student_Table
            List<Student> student=(List<Student>)query.list();
            for(Student s:student)
                System.out.println(s.getSname());
            trans.commit();
            session.close();
        }
        catch(Exception e)
        {
            
        }
        
    }
    
}

In the above code, we have used “from Student” –> This code is similar to SQL query – Select * from Student_Table
Note that, We have not used table name instead we have used Entity name ‘Student’. In HQL, Instead of Table name we always use Entity name.

Output: 
op1

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *