Hibernate Query Language, Using HQL Select Query

Let us see the program on HQL select command,  which is going to cover complete object, partial object (More than one column), partial object (Single column)

here are the required files….

  • Product.java (POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • ForOurLogic.java (java file to write our hibernate logic)
  • Product.java

    1234567891011121314151617181920212223242526272829303132333435package str;
    
    public class Product{
    
    	private int productId;
    	private String proName;
    	private double price;
    
    	public void setProductId(int productId)
    	{
    	    this.productId = productId;
    	}
    	public int getProductId()
    	{
    	    return productId;
    	}
    
    	public void setProName(String proName)
    	{
    	    this.proName = proName;
    	}
    	public String getProName()
    	{
    	    return proName;
    	}
    
    	public void setPrice(double price)
    	{
    	    this.price = price;
    	}
    	public double getPrice()
    	{
    	    return price;
    	}
    }

    hibernate.cfg.xml

    1234567891011121314151617181920<?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
    </property>
    <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
    <property name="connection.username">system</property>
    <property name="connection.password">admin</property>
    
    <property name="dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    
    <mapping resource="Product.hbm.xml"></mapping>
    </session-factory>
    </hibernate-configuration>

     

    Product.hbm.xml

    1234567891011121314<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    <class name="str.Product" table="products">
    
    <id name="productId" column="pid"  />
    <property name="proName" column="pname" length="10"/>
    <property name="price"/>
    
    </class>
    </hibernate-mapping>

    ForOurLogic.java

    12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788package str;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import java.util.*;
    
    public class ForOurLogic { 
    
    	public static void main(String[] args)
    	{
    
    		Configuration cfg = new Configuration();
    		cfg.configure("hibernate.cfg.xml"); 
    
    		SessionFactory factory = cfg.buildSessionFactory();
    		Session session = factory.openSession();
    
    

    /* Selecting all objects(records) start_______________________ */

    /* Query qry = session.createQuery("from Product p"); List l =qry.list(); System.out.println("Total Number Of Records : "+l.size()); Iterator it = l.iterator(); while(it.hasNext()) { Object o = (Object)it.next(); Product p = (Product)o; System.out.println("Product id : "+p.getProductId()); System.out.println("Product Name : "+p.getProName()); System.out.println("Product Price : "+p.getPrice()); System.out.println("----------------------"); } */

    /* Selecting all objects(records) end________________________ */

    /* Selecting partial objects(More than one object) start__________ */

    /* Query qry = session.createQuery("select p.productId,p.proName from Product p"); List l =qry.list(); System.out.println("Total Number Of Records : "+l.size()); Iterator it = l.iterator(); while(it.hasNext()) { Object o[] = (Object[])it.next(); System.out.println("Product id : "+o[0]+ "Product Name : "+o[1]); System.out.println("----------------"); } */

    /* Selecting partial objects(More than one object)end_____________ */

    // Selecting single object start_____________

    Query qry = session.createQuery("select p.productId from Product p"); List l =qry.list(); System.out.println("Total Number Of Records : "+l.size()); Iterator it = l.iterator(); while(it.hasNext()) { Integer i = (Integer)it.next(); System.out.println("Product id : "+i.intValue()); System.out.println("---------------------------"); }

    // selecting single object end____________

    session.close(); factory.close(); } }

    Notes regarding ForOurLogic.java:

  • CASE 1:  For selecting complete objects (all rows from the table), see from line number 18 – 40 [ just remove the comments ] and check the out put
  • CASE 2:  For selecting partial objects, i mean more than one object, see from line number 42 – 63 [ just remove the comments ] and check the out put.
  • Product.java

    1234567891011121314151617181920212223242526272829303132333435package str;
    
    public class Product{
    
    	private int productId;
    	private String proName;
    	private double price;
    
    	public void setProductId(int productId)
    	{
    	    this.productId = productId;
    	}
    	public int getProductId()
    	{
    	    return productId;
    	}
    
    	public void setProName(String proName)
    	{
    	    this.proName = proName;
    	}
    	public String getProName()
    	{
    	    return proName;
    	}
    
    	public void setPrice(double price)
    	{
    	    this.price = price;
    	}
    	public double getPrice()
    	{
    	    return price;
    	}
    }

    hibernate.cfg.xml

    1234567891011121314151617181920<?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
    </property>
    <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
    <property name="connection.username">system</property>
    <property name="connection.password">admin</property>
    
    <property name="dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    
    <mapping resource="Product.hbm.xml"></mapping>
    </session-factory>
    </hibernate-configuration>

     

    Product.hbm.xml

    1234567891011121314<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    <class name="str.Product" table="products">
    
    <id name="productId" column="pid"  />
    <property name="proName" column="pname" length="10"/>
    <property name="price"/>
    
    </class>
    </hibernate-mapping>

    ForOurLogic.java

    12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788package str;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import java.util.*;
    
    public class ForOurLogic { 
    
    	public static void main(String[] args)
    	{
    
    		Configuration cfg = new Configuration();
    		cfg.configure("hibernate.cfg.xml"); 
    
    		SessionFactory factory = cfg.buildSessionFactory();
    		Session session = factory.openSession();
    
    

    /* Selecting all objects(records) start_______________________ */

    /* Query qry = session.createQuery("from Product p"); List l =qry.list(); System.out.println("Total Number Of Records : "+l.size()); Iterator it = l.iterator(); while(it.hasNext()) { Object o = (Object)it.next(); Product p = (Product)o; System.out.println("Product id : "+p.getProductId()); System.out.println("Product Name : "+p.getProName()); System.out.println("Product Price : "+p.getPrice()); System.out.println("----------------------"); } */

    /* Selecting all objects(records) end________________________ */

    /* Selecting partial objects(More than one object) start__________ */

    /* Query qry = session.createQuery("select p.productId,p.proName from Product p"); List l =qry.list(); System.out.println("Total Number Of Records : "+l.size()); Iterator it = l.iterator(); while(it.hasNext()) { Object o[] = (Object[])it.next(); System.out.println("Product id : "+o[0]+ "Product Name : "+o[1]); System.out.println("----------------"); } */

    /* Selecting partial objects(More than one object)end_____________ */

    // Selecting single object start_____________

    Query qry = session.createQuery("select p.productId from Product p"); List l =qry.list(); System.out.println("Total Number Of Records : "+l.size()); Iterator it = l.iterator(); while(it.hasNext()) { Integer i = (Integer)it.next(); System.out.println("Product id : "+i.intValue()); System.out.println("---------------------------"); }

    // selecting single object end____________

    session.close(); factory.close(); } }

    Notes regarding ForOurLogic.java:

  • CASE 1:  For selecting complete objects (all rows from the table), see from line number 18 – 40 [ just remove the comments ] and check the out put
  • CASE 2:  For selecting partial objects, i mean more than one object, see from line number 42 – 63 [ just remove the comments ] and check the out put
  • CASE 3:  For selecting partial objects,  (selecting single row), see from line number 65 – 82 [ just remove the comments ] and check the out put
  • See the following output just am giving for CASE 3, and remember in case1 i typecast into POJO class type, in case 2 typecast into objects array, in case 3 typecast into that value type__ (in the while loop..)

 

Related Articles

post a comment