Common Aggregate functions of Hibernate

Source: Internet
Author: User

Common Aggregate functions of Hibernate

Package entity;
// Persistence class
Import javax. persistence. Entity;
Import javax. persistence. GeneratedValue;
Import javax. persistence. GenerationType;
Import javax. persistence. Id;
Import javax. persistence. Table;

// HQl query book information
@ Entity
@ Table
Public class HqlQuery {
Private int id;
Private String name;
Private String author;
@ Id
@ GeneratedValue (strategy = GenerationType. IDENTITY)
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getAuthor (){
Return author;
}
Public void setAuthor (String author ){
This. author = author;
}

Public HqlQuery (){
Super ();
}
Public HqlQuery (String name, String author ){
Super ();
This. name = name;
This. author = author;
}
}

// Five functions:

Package test;

Import org. hibernate. Session;
Import org. hibernate. Transaction;

Import util. HibernateUtil;

Public class FunTest {

Public static void main (String [] args ){
// Hcount ();
// Hsum ();
// Hmax ();
// Hmin ();
Havg ();
}
Public static void hcount (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "select count (*) from HqlQuery ";
Long n = (Long) session. createQuery (hql). uniqueResult ();
System. out. println ("number of records:" + n );
}
Public static void hsum (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "select sum (id) from HqlQuery ";
Long n = (Long) session. createQuery (hql). uniqueResult ();
System. out. println ("and:" + n );
}
Public static void hmax (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "select max (id) from HqlQuery ";
Int n = (Integer) session. createQuery (hql). uniqueResult ();
System. out. println ("Maximum id:" + n );
}
Public static void hmin (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "select min (id) from HqlQuery ";
Int n = (Integer) session. createQuery (hql). uniqueResult ();
System. out. println ("minimum id:" + n );
}
Public static void havg (){
Session session = HibernateUtil. getCurrentSession ();
Transaction transaction = session. beginTransaction ();
String hql = "select avg (id) from HqlQuery ";
Double n = (Double) session. createQuery (hql). uniqueResult ();
System. out. println ("average id:" + n );
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.