HQL Select Query Hibernate

HQL Select Query Hibernate

Hibernate framework given very powerful query language Hibernate Query Language (HQL) which is similar to SQL only difference is HQL is object oriented and it can recognize notation of object oriented concept associate, polymorphism and inheritance. HQL uses class name in place of table name to execute query against the database. Please have HQL select query example below to select data from database:

Note: This class is extension of previous tutorial. If you are interested to see full working example with all set up please refer this tutorial Hibernate One To Many Annotation Example

package com.javahonk;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.javahonk.bean.Child;
import com.javahonk.bean.Mother;
import com.javahonk.util.HibernateUtil;

public class HQLSelect {
    
    static SessionFactory sf = HibernateUtil.getSessionFactory();

    public static void main(String[] args) {
        
        Session session = sf.openSession();
                
        System.out.println("Select query example 1\n");
        
        Query query = session.createQuery("from Mother where "
                + "motherId = :motherId");
        query.setInteger("motherId", 8);
        
        List<Mother> list = query.list();
        
        for (Mother mother : list) {
            System.out.println("\nMother table data:");
            System.out.println("First Name: "+mother.getFirstName()
                    +" Last Name: "+mother.getLastName()+" City: "
                    +mother.getCity()+" State: "+mother.getState()
                    +" Zip: "+mother.getZip()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother.getChilds();
            for (Iterator<Child> iterator = childs.iterator(); 
                    iterator.hasNext();) {
                Child child3 = (Child) iterator.next();
                System.out.println("First Name: "
                +child3.getFirstName()+" Last name: "
                        +child3.getLastName()+"\n");
    
            }
    
        }
        
        System.out.println("Select query example 2\n");
        
        query = session.createQuery("from Mother where "
                + "motherId = 8");
                
        list = query.list();
        
        for (Mother mother : list) {
            System.out.println("\nMother table data:");
            System.out.println("First Name: "+mother.getFirstName()
                    +" Last Name: "+mother.getLastName()+" City: "
                    +mother.getCity()+" State: "+mother.getState()
                    +" Zip: "+mother.getZip()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother.getChilds();
            for (Iterator<Child> iterator = childs.iterator(); 
                    iterator.hasNext();) {
                Child child3 = (Child) iterator.next();
                System.out.println("First Name: "
                +child3.getFirstName()+" Last name: "
                        +child3.getLastName()+"\n");
    
            }
    
        }   
        
        session.close();

    }

}

Output:

HQL Select Query Hibernate

 

For more information about HQL please read this hibernate official tutorial

Leave a Reply

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