In this section, you will learn about the hibernate projections with an appropriate example.
Projections: The package Criteria is used as a
framework by the applications just to build the new kinds of projection. may be used
by applications as a framework for building new kinds of Projection. In
general Projection means to retrieve while in case of SQL Projection means
"Select" clause. Most of the applications uses the built-in
projection types by means of the static factory methods of this class.
ProjectionList: is the list of projection
instances which are result of Query's object.
Criteria API: enables us to specify criteria based on various.
In the class projectionExample.java, first
we create the session object with the help of the SessionFactory interface. Then
we use the createQuery() method of the Session object which returns a Query
object. Now we use the openSession() method of the SessionFactory interface
simply to instantiate the Session object.
Then we obtain the criteria object simply by invoking the createCriteria() method of the Session's object. Now we create a projectionList object add the fields having properties "name" and "price". Set it to the Criteria object by invoking the setProjection() method and passing the projectList object into this method and then add this object into the List interface's list object and iterate this object list object to display the data contained in this object.
|log4j:WARN No appenders
could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select this_.name as y0_, this_.price as y1_ from Product this_
Product Name Price
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial