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"?> |
A unidirectional one-to-many association on a foreign key is rarely required.
<?xml version='1.0' encoding='utf-8'?> |
Here is the hibernate code:
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;
|
Output:
| 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 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Hibernate Projection Count View All Comments
Post your Comment