Newsletter

Example On Hibernate Criteria With Multiple Projections

Hibernate » on Jul 13, 2011 { 13 Comments } By Sivateja

If we want to load partial object, with multiple columns using criteria then we need to create the ProjectionList with the multiple properties and then we need to add that ProjectionList to the criteria

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.ProjectionList;
import org.hibernate.criterion.Projections;

public class ForOurLogic_2 { 

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

		ProjectionList p1=Projections.projectionList();
		         p1.add(Projections.property("proName"));
		         p1.add(Projections.property("price"));	

		crit.setProjection(p1);		

		List l=crit.list();

		Iterator it=l.iterator();

		while(it.hasNext())
		{
			Object ob[] = (Object[])it.next();
			System.out.println(ob[0]+"--------"+ob[1]);
		}

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

}

Notes:

  • See line numbers 27,28,29, for using multiple projections we must create this ProjectionList object then we have to add all of our projections to ProjectionList, then finally add this ProjectionList object to criteria (see line number 31)
  • Now see line number 39, Finally we have to typecast into object array
  • Remember, we have to typecast into our pojo class type if we load complete object from the database,  we have to typecast into that property type if we load partial object (single column), we have to typecast into object array if we load partial object (more than single column)

hibernate projections, hibernate multiple projections, example on hibernate projections, projections in hibernate,

​​

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

13 Responses to “Example On Hibernate Criteria With Multiple Projections”
  1. hi java4s team ,
    u have given excellent explaination . i got it , thanks a lot .

  2. vimal says:

    can you please send some interview questions based on “versioning in hibernate” ………..

  3. Anasuya says:

    Please give some real time interview questions on Hibernate…
    Thanks a million for the good tutorial…

  4. Tipu Swain says:

    Hi Dude,

    Really brilliant explanation.

    Thanks a lot.

  5. Don says:

    Easy to understand Thanks for sharing

  6. karthick says:

    u r really genius …best explanation…:)

  7. karthick says:

    nice tips in the remember section…..

  8. appesh says:

    Insted of Iterating Object we can type case into Specifed Object eg: p1.add(Projections.property(“price”).setResultTransformer(Transformers.aliasToBean(XXXX.class));

  9. Evyavan says:

    First of all let me say that the post is a nice one. I am here. shamelessly, to ask for a helping hand. I need to figure out something and if you can and time permits please give me an answer for this question. http://stackoverflow.com/questions/27099693/will-aliastobean-work-when-a-dto-has-a-list-of-another-dto-and-fields-set-with

    I am 95% sure this cannot be done how I have coded it, but I sure hope there is a way around. Please excuse my ignorance as I am still learning the ropes of hibernate.

  10. srikanth says:

    I thought, found one bug in this post or i did not understand properly.

    In previews example (adding one prjection to criteria) used below statement,
    crit.setProjection(Projections.property(“proName”));
    i.e, crit.setProjection() with action one Projections object.

    Now, again you used same criteria method to add projectionList like below

    ProjectionList p1=Projections.projectionList();
    p1.add(Projections.property(“proName”));
    p1.add(Projections.property(“price”));

    crit.setProjection(p1);

    p1 is projectionList one projections, so how this crit.setProjection() method accept that,

    This is my doubt.

  11. shgy says:

    Thanks!
    Very nice Article!

  12. suryas says:

    I have learnt hibernate f/w with your articles.
    it is too good.heartly thanksful to you.

  13. Vaishali says:

    One of the best hibernate tutorial.
    Great explanation!

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.