Newsletter

Hibernate Named Query Example

Hibernate » on Jul 14, 2011 { 4 Comments } By Sivateja

Let us see an example on Hibernate Named Queries,

files required…

  • Product.java(POJO class)
  • Product.hbm.xml
  • hibernate.cfg.xml
  • ForOurLogic.java (For writing our business logic)

 

Product.java

package 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;
	}
}

Product.hbm.xml

<?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>

<query name="Myquery">
<![CDATA[from Product p where p.price>:java4s ]]>
</query>

</hibernate-mapping>

hibernate.cfg.xml

<?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>

ForOurLogic.java

package str;

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

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

public class ForOurLogic { 

	@SuppressWarnings("unchecked")
	public static void main(String[] args)
	{

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();

		Query qry=session.getNamedQuery("Myquery");
		qry.setParameter("java4s", new Double(15000));

		List l=qry.list();
		System.out.println("List total size..._"+l.size());
		Iterator it=l.iterator();

		while(it.hasNext())
		{
			Product p=(Product)it.next();
			System.out.println(p.getProductId()+"   "+p.getProName()+"  "+p.getPrice());
			System.out.println("-----------------");
		}

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

}

Notes:

  • See Product.hbm.xml file, line numbers 15, 16, 17, we have given one HQL query in the <query /> element
  • In ForOurLogic.java file, line number 23 will get that query from mapping xml into Query object, in line 24 we just passed the value to that label normally

But if you would like to use Named Query with Native SQL, then we need to change our mapping file like…

 

Product.hbm.xml

<?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>

<sql-query name="Myquery">
  select * from PRODUCTS
</sql-query>

</hibernate-mapping>

Note: In this case we need to use one new element <sql-query /> unlike <query /> element which we used to write HQL query

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

4 Responses to “Hibernate Named Query Example”
  1. sandilya says:

    How to validate to fields in a table from the given values in hibernate for JavaFX using eclipse
    For ex:
    Username = “********”
    Password = “*******”

    I want to know whether the given values are same as in the table.

  2. karthick says:

    nice tutorial…very useful to me….

  3. mike says:

    How to perform the same with projection in the query like
    “select ab.pname from Products ab ”
    ?

  4. Azad says:

    Nice explanation and easy example Great!!!!.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.