Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Hibernate Hibernate criteria count.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ratna rathor
Hibernate criteria count.
2 Answer(s)      2 years and 5 months ago
Posted in : Hibernate


How do we count rows using criteria in hibernate?


Advertisement
View Answers

May 28, 2012 at 6:30 PM


Create hibernate configuration file (hibernate.cfg.xml) and save it in the same folder where you are going to save your source code.

Now create Persistent class ?Hibernate uses the Plain Old Java Object (POJO) classes to map to the database table. Here is the code of Employee.java ? Next create an util class as HibernateUtil.java Here is your MainClass-

package roseindia.application;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import roseindia.table.Employee;
import roseindia.util.HibernateUtil;

public class MainClass{
     public static void main(String[] args) {

    SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    Session session=sessionFactory.openSession();
    Criteria criteria=session.createCriteria(Employee.class);
    criteria.setProjection(Projections.rowCount());
    List list = criteria.list();
    System.out.println("Number of employee : "+list.get(0));

}
}

Description: Hibernate criteria is used here as alternative to HQL. In general an application has a single sessionFactory. If you are using multiple databases then you must have sessionFactory for each database. SessionFactory is configured in your configuration file hibernate.cfg.xml.

Projections.rowCount() counts all the rows of your table.



May 29, 2012 at 12:04 AM


  1. run your criteria.list() without setting any alias => the referenced sets/list of the root entity will be filled with proxies => here you set correctly the max results and such
  2. run the alias criteria on its own in the same hibernate session => the above proxies will be initialized

Code:

Criteria criteria = this.getSession().createCriteria(User.class);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
criteria.setMaxResults(10);

// First get the results without joining with the other tables
List<User> results = criteria.list();

// at this point the set roles is filled with proxies
// we'll now create and execute the join so these proxies are filled since we're still in the same session
getSession().createCriteria(User.class, "u")
        .createAlias("u.roles", "r", CriteriaSpecification.LEFT_JOIN)
        .list();

return results;


Related Tutorials/Questions & Answers:
Hibernate Criteria Count Example
Hibernate Criteria Count Example  Example of Hibernate Criteria Count. Check the tutorial Hibernate Criteria Count. Check the Latest tutorials, articles and examples of Hibernate Framework. Thanks
Hibernate criteria count.
Hibernate criteria count.  How do we count rows using criteria in hibernate?   We can get the rows count using the Projections. My code is like this: Criteria criteria = session.createCriteria(getReferenceClass
Advertisements
Hibernate criteria count.
Hibernate criteria count.  How do we count rows using criteria in hibernate?   Create hibernate configuration file (hibernate.cfg.xml...(0)); } } Description: Hibernate criteria is used here as alternative to HQL
Hibernate Criteria Count Distinct Example
Hibernate Criteria Count Distinct Example  I am learning Hibernate... Criteria Count Distinct. Can anyone tell me the best place to learn Hibernate Criteria Count Distinct? Thanks   Here is an example of Hibernate Criteria
Hibernate Criteria Count
Hibernate Criteria Count The Hibernate Criteria Count is used to count the numbers of records in a table. To Count a number of records in a table make...().openSession(); Criteria criteria = session.createCriteria(Student.class
Hibernate Criteria Count Distinct
Hibernate Criteria Count Distinct The Hibernate Criteria count distinct is used to count the number of distinct records in a table. Following a way to use it in a program Criteria criteria = session.createCriteria(Student.class
count in hibernate .
count in hibernate .  How to implements count in hibernate
Criteria in Hibernate
Criteria in Hibernate  What is Criteria in Hibernate?   Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens
Hibernate Criteria Examples
Hibernate Criteria Count Hibernate Criteria Count Distinct Hibernate Criteria Case...Hibernate Criteria Examples In this section we are giving many example of using Hibernate Criteria query. With the help of criteria query you can
Hibernate criteria disjunction.
Hibernate criteria disjunction.  What is criteria disjunction in hibernate
Hibernate Criteria And Or Example
Hibernate Criteria And Or Example  Hibernate Criteria And Or Example I want example of Hibernate Criteria And Or in Java Thanks   Example of Hibernate Criteria And Or Criteria Check the tutorial Hibernate Criteria
Hibernate-Criteria - Hibernate
Hibernate-Criteria  Hi friends, I am new to hibernate. Im learning it. When im using Criteria im getting exception tht "Criteria cannot... for more information. http://www.roseindia.net/hibernate/hibernate-between
Hibernate Criteria Or
In this section we are going to discuss Hibernate Criteria Or with example
Hibernate Criteria
In this tutorial we are going to discuss Hibernate Criteria with example
Hibernate criteria delete Example
Hibernate criteria delete Example  How to use the delete criteria in Hibernate? thanks
Hibernate criteria by id.
Hibernate criteria by id.  How to display record by using criteria by id in Hibernate
Hibernate criteria date Example
Hibernate criteria date Example  Example of Hibernate criteria date. Check the tutorial hibernate criteria date. Check the Latest tutorials, articles and examples of Hibernate Framework. Thanks
Hibernate Criteria With Collections Example
Hibernate Criteria With Collections Example  Good code of Hibernate... Hibernate Criteria With Collections in your Java project. Check the tutorial Hibernate Criteria With Collections . Check the Latest tutorials, articles
Hibernate Criteria And Restrictions Example
Hibernate Criteria And Restrictions Example  How to use Criteria And Restrictions in Hibernate? Thanks   I will provide you the example code of Hibernate Criteria And Restrictions. Check the tutorial Hibernate
Hibernate Criteria Wildcard
Hibernate Criteria Wildcard   How to write Criteria Query in Hibernate that uses the Wildcards?   Hello, Check the example Hibernate Criteria Wildcard. Thanks
What are criteria Queries in Hibernate?
What are criteria Queries in Hibernate?  Just explain me what is Criteria Query in Hibernate with examples? Thanks   Criteria queries are very helpful in hibernate for queries based on certain criteria. Check
Hibernate Criteria Queries - Hibernate
Hibernate Criteria Queries  Can I use the Hibernate Criteria Query...().buildSessionFactory(); session = sessionFactory.openSession(); Criteria crit... Configuration(); // configuring hibernate SessionFactory
What is Hibernate Criteria Transformer?
What is Hibernate Criteria Transformer?  Hi, Can anyone explain me about the Hibernate Criteria Transformer? I need good example of Hibernate Criteria Transformer . Thanks   Hi, check the example Hibernate Criteria
Hibernate criteria for date. Example
Hibernate criteria for date. Example  What to use the Hibernate criteria for date in Hibernate based application? I want example of Hibernate criteria for date in Hibernate orm. Thanks   Example of Hibernate criteria
Hibernate criteria average Example
Hibernate criteria average Example   How to find the average in Hibernate? Thanks   Example of finding average on a filed in Hibernate... the tutorial Hibernate criteria avg.. Check the Latest tutorials, articles
Hibernate Criteria Between Example
Hibernate Criteria Between Example  How to use the Between in the Hibernate Query? Share me good example code. Thanks   Example of Hibernate Criteria Between. Check the tutorial Hibernate Criteria Between. Check
hibernate criteria disjunction Example
hibernate criteria disjunction Example  I want example of hibernate criteria disjunction. Thanks   Example of hibernate criteria disjunction. Please check the tutorial hibernate criteria disjunction. This tutorial
Hibernate Detached Criteria Example
Hibernate Detached Criteria Example  How to use Hibernate Detached Criteria in Java program? What is the use of Hibernate Detached Criteria? Explain... Detached Criteria. Check Latest Hibernate Tutorials and Articles
Hibernate Criteria Wildcard Example
Hibernate Criteria Wildcard Example  Hi, How to write Java program to use the Hibernate Criteria Wildcard query and fetch the data from database I want example of Hibernate Criteria Wildcard which is easy to understand
Hibernate Detached Criteria Example
Hibernate Detached Criteria Example  can anyone share me the source code of using detached Criteria in Hibernate? Thanks   Hi, If you are looking for the Example program of using Hibernate Detached Criteria
Criteria Query Examples in Hibernate
Criteria Query Examples in Hibernate  How to use the Criteria Query in Hibernate? How to select the data using Criteria Query of Hibernate? Thanks... Criteria Query in Hibernate with Examples code. Check the tutorial Criteria Query
Advertisements
 

 

 

DMCA.com