Hibernate Named Query

Hibernate Named Query

Hibernate named SQL queries can be defined in mapping file or as annotation and called in same way as named HQL query. Below are examples:
Note: This demo 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

  • Using HQL annotation:
@NamedQueries({ @NamedQuery(name = "findMotherNameById", 
        query = "from Mother where mother_id = :mother_id") })
  • Using Native SQL annotation:
@NamedNativeQueries({ @NamedNativeQuery(
        name = "findMotherNameByIdUsingNativeSQL", 
        query = "select * from Mother where mother_id = 1", 
        resultClass = Mother.class) })
  • Using HQL XML mapping:
<query name="findMotherNameById">
        <![CDATA[from Mother where mother_id = :mother_id]]>
    </query>

OR

<query name="findMotherNameById2">
        from Mother where mother_id = :mother_id
</query>

Using Native SQL XML mapping:

<sql-query name="findMotherNameByIdUsingNativeSQL">
        <return alias="mother" class="com.javahonk.bean.Mother" />
        SELECT * FROM mother WHERE mother_id = :mother_id       
</sql-query>

OR

<sql-query name="findMotherNameByIdUsingNativeSQL2">
        <return alias="mother" class="com.javahonk.bean.Mother" />
        <![CDATA[select * from mother where mother_id = :mother_id]]>
</sql-query>
  • Complete java code 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 HQLNamedQuery {
    
    static SessionFactory sf = HibernateUtil.getSessionFactory();   

    public static void main(String[] args) {

        Session session = sf.openSession();
        session.beginTransaction();
       
        //Named query - HQL example
        Query query = session.getNamedQuery("findMotherNameById");
        query.setString("mother_id", "1");
        List<Mother> moList = query.list();
        
        for (Mother mother2 : moList) {
            System.out.println("Mother table data:");
            System.out.println("First Name: "+mother2.getFirstName()
                    +" Last Name: "+mother2.getLastName()+" City: "
                    +mother2.getCity()+" State: "+mother2.getState()
                    +" Zip: "+mother2.getZip()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother2.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");

            }

        }
        
        //Named query - Native SQL example
        query = session.getNamedQuery(
                "findMotherNameByIdUsingNativeSQL");
        query.setString("mother_id", "1");
        moList = query.list();
        
        for (Mother mother2 : moList) {
            System.out.println("Mother table data:");
            System.out.println("First Name: "+mother2.getFirstName()
                    +" Last Name: "+mother2.getLastName()+" City: "
                    +mother2.getCity()+" State: "+mother2.getState()
                    +" Zip: "+mother2.getZip()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother2.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.getTransaction().commit();
        session.close();

    }

    
}

Output:

Hibernate Named Query

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 *