Newsletter

Hibernate Projections, Example On Hibernate Projections

Hibernate » on Jul 13, 2011 { 1 Comment } By Sivateja

Now we will see how to use this criteria to select partial objects from the database with the help of projections, (Remember, We cant select partial objects in criteria with out projections support)

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>
</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.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;

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

		Criteria crit = session.createCriteria(Product.class);

/*
		crit.setProjection(Projections.property("proName"));
		List l=crit.list();
		Iterator it=l.iterator();

		while(it.hasNext())
		{
			String s=(String)it.next();
			System.out.println(s);
		}

*/	

		crit.setProjection(Projections.property("price"));
		List l=crit.list();
		Iterator it=l.iterator();

		while(it.hasNext())
		{
			Double s=(Double)it.next();
			System.out.println(s);
		}

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

}

Notes:

  • In the above ForOurLogic.java line numbers 2637 , we loaded product name column so i typecast into String  in while loop
  • but from 39, i loaded price column, actually price always used to be in the form of double right so, we must typecast into double

And that’s it.., hope you clear about this concept (make sure you must see the previous sessions then only you will understand this program 100%), let us see the Example Program On Hibernate Criteria With Multiple Projections in the next session

​​

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

One Response to “Hibernate Projections, Example On Hibernate Projections”
  1. INDIAN says:

    Nice Tutorial

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.