Newsletter

Hibernate Projections Introduction

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

So far in criteria, we are able to load complete object right….! let us see how to load the partial objects while working with criteria.Β  The projections concept is introduced in hibernate 3.0 and mainly we can do the following 2 operations using the projection

  • We can load partial object from the database
  • We can find the Result of Aggregate functions

Projection is an Interface given in “org.hibernate.criterion” package, Projections is an class given in same package,Β  actually Projection is an interface, and Projections is an class and is a factory for producing projection objects.

In Projections class, we have all static methods and each method of this class returns Projection interface object.

If we want to add a Projection object to Criteria then we need to call a method setProjection()

Remember, while adding projection object to criteria, it is possible to add one object at a time.Β  It means if we add 2nd projection object then this 2nd one will overrides the first one (first one wont be work), so at a time we can only one projection object to criteria object.

Using criteria, if we want to load partial object from the database, then we need to create a projection object for property that is to be loaded from the database

Example:

Criteria crit = session.createCriteria(Products.class);
crit.setProjection(Projections.proparty("proName"));
List l=crit.list();
Iterator it=l.iterator();
while(it.hasNext())
{
String s = (String)it.next();
// ---- print -----
}

If we add multiple projections to criteria then the last projection added will be considered to execute see…

Example:

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

Projection p1 = Projection.property("proName");
Projection p2 = Projection.property("price");

crit.setProjection(p1):
crit.setProjection(p2):
List l=crit.list();
-------------- ----
------- - ---  ----

Here collections list l, is going to contain the price in the form of Double objects, but product names are over ridden,Β  second projection over rides the first one, i mean p2 only will works p1 will not works, actually there is a way to add multiple projections to criteria to select more than one column from the database i will tell you in the next example πŸ™‚

​ ​​

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

10 Responses to “Hibernate Projections Introduction”
  1. sankar says:

    Excellent information

  2. prasad says:

    Hi Java4s,
    Actually partial objects means fetching some of the columns from table instead of fetching all. That is what you mean to say right!

    • Nagul Saida says:

      Projections help you filter data and return only those fields you need. You can use projections when
      you don’t want the entire fetched entity and its associations.

  3. Rahul says:

    here in 2nd case..it should be like pojections.property(“proName”)
    or projection.property(“proName”)
    because property is a static method of Projections class
    do help me out on this

  4. Ram says:

    spelling mistake at
    crit.setProjection(Projections.proparty(“proName”));
    that is property not proparty

  5. Anand Parokottil says:

    In the 2nd example, on 3rd and 4th line i have noticed a mistake. Kindly change it @SivaTeja sir.
    Instead of,
    Projection’s’.property(“proName”);
    Projection’s’.property(“price”);
    you have used,
    Projection.property(“proName”);
    Projection.property(“proName”);

    Your tutorial is really interesting. All the best πŸ™‚

  6. Muralli says:

    We cannot create objects to an interface. Pls explain what the projections class return then and how?

  7. pankaj says:

    Good Explanation. and good Example.

  8. Rohit says:

    We cannot create objects to an interface. Please explain what the projections class return then and how?

  9. ravendra singh says:

    Projection p1 = Projection.property("proName");
    Projection p2 = Projection.property("price");

    not a valid objection creation becoz projection is not have any method …
    ex-
    Projection p1 = Projections.property("proName");
    Projection p2 = Projections.property("price");

    it is a proper working..
    thanks

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.