Hibernate count rows with some criterias

后端 未结 6 969
生来不讨喜
生来不讨喜 2021-02-07 15:16

Suppose I have a table Person and i want to count all those people whose \"birthDate\" is not null and they are a student. Assuming i have two columns :

birthDat         


        
相关标签:
6条回答
  • 2021-02-07 15:29
    Criteria crit = session.createCriteria(Person.class);
    crit.add( Restrictions.isNotNull("birthDate"));
    crit.add( Restrictions.eq("isStudent", true));
    List<Person> students = crit.list();
    Integer count = students.size();
    

    or if just want a single count value, and no list returned :

    Criteria crit = session.createCriteria(Person.class);
    crit.setProjection(Projections.rowCount());
    crit.add( Restrictions.isNotNull("birthDate"));
    crit.add( Restrictions.eq("isStudent", true));
    return (Long) crit.uniqueResult(); 
    
    0 讨论(0)
  • 2021-02-07 15:39
    Criteria crit = session.createCriteria(Person.class);
    crit.add( Restrictions.isNotNull("birthDate"));
    crit.add( Restrictions.eq("isStudent", true));
    crit.setProjection(Projections.rowCount());
    Integer count = (Integer)crit.uniqueResult();
    
    0 讨论(0)
  • 2021-02-07 15:41

    Use it. It's working

    public class IncIncidentListGridModel
    {
    
        private String historyStatus = null;
    
        public String getHistoryStatus()
        {
            return historyStatus;
        }
    
        public void setHistoryStatus(String historyStatus)
        {
            this.historyStatus = historyStatus;
        }
    
        public void run()
        {
            IncIncidentListGridModel resultCount =
                (IncIncidentListGridModel) ((SQLQuery) hibersession
                    .createSQLQuery("select count(ch.incident_capa_history_id) as historyStatus  from incident_capa_history ch, incident_capa ic where ic.incident_capa_id = ch.FK_incident_capa_id and ic.incident_capa_id='011'"))
                    .addScalar("historyStatus", Hibernate.STRING)
                    .setResultTransformer(
                        Transformers.aliasToBean(IncIncidentListGridModel.class))
                    .uniqueResult();
    
            System.out.println("count result" + resultCount.getHistoryStatus());
        }
    }
    
    0 讨论(0)
  • 2021-02-07 15:44
    int result= (int)((long)session.createQuery("select count(p) from User p where p.mobileNumber = :pMobileNumber")
                     .setParameter("pMobileNumber", mobileNumber).uniqueResult());
    
    0 讨论(0)
  • 2021-02-07 15:44
    Criteria crit = session.createCriteria(Person.class);
    crit.setProjection(Projections.rowCount());
    crit.add( Restrictions.isNotNull("birthDate"));
    crit.add( Restrictions.eq("isStudent", true));
    return (Long) crit.uniqueResult();
    

    The (Long)crit.uniqueResult() will cause NonUniqueResultException. I check the crit.list(). It is an ArrayList having 2 items. The first item of the arraylist contains the count. The second item always contains a 0. Is it the expected result of rowCount?

    Thanks

    0 讨论(0)
  • 2021-02-07 15:47
    Number count = (Number) session.createQuery(
        "select count(p.id) from Person p"
        + " where p.birthDate is not null and p.isStudent = true").uniqueResult();
    
    0 讨论(0)
提交回复
热议问题