Hibernate Projection Count
In this section, you will learn to hibernate projection count. The example demonstrates the rowCount() method of the projection interface.A unidirectional one-to-many association on a foreign key is rarely required.
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version='1.0' encoding='utf-8'?> |
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 set the number of rows count simply by calling the setProjection() method of the Criteria APIs and passing it the rowCount() method of the Projections interface. Then pass the reference of the Criteria object into the List interface object and finally iterate this object to count the number of rows.
package net.roseindia; |
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).log4j:WARN Please initialize the log4j system properly.Hibernate: select count(*) as y0_ from Product this_Row: 7 |
No comments:
Post a Comment