Hibernate Query Language, Passing Runtime Values

Now we will see, how to pass the values at time time while using the HQL  select query, actually same concept for 3 cases.

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

 

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162package 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();		

/* Using label...............

Query qry = session.createQuery("from Product p where p.productId= :java4s"); qry.setParameter("java4s",105); 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 Name : "+p.getProName()); System.out.println("Product Price : "+p.getPrice()); System.out.println("---------------------------"); }

*/

/* Using Question Mark */

Query qry = session.createQuery("from Product p where p.productId= ?");

qry.setParameter(0,105); 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 Name : "+p.getProName()); System.out.println("Product Price : "+p.getPrice()); System.out.println("---------------------------"); } session.close(); factory.close(); } }

 

Eclipse Console

In The Database

Notes:

  • As i discussed earlier, in the hibernate we can pass the values into the hibernate query at run time also
  • See the commented code from line numbers 18 – 39,  that if we would like to use Label rather question mark symbol
  • See the HQL query in line number 20, i have been used label :java4s [ as a programmer its our responsible to let the hibernate to know its the label not the value, by specifying the colon(:) symbol at starting of label ]
  • In line number 21 i used setParameter method to give the value that’s it.
  • And if you observer line number 41, i used question mark symbol rather label, this time in setParameter method i used zero (0) as first parameter and value in second parameter, remember if use more than one question mark symbol in the query then also the index will starts from zero only

Related Articles

post a comment