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



Home Answers Viewqa Hibernate Using Min, Max and Count on HQL.

Have Programming Question? Ask it here!
 
 
 


ratna rathor
Using Min, Max and Count on HQL.
2 Answer(s)      2 years and 3 months ago
Posted in : Hibernate


How to use Min, Max and Count on HQL ?


Advertisement
View Answers

June 1, 2012 at 7:13 PM


HQL supports many aggregate functions. Min,max and count are some of them.

min() function returns minimum value of specified value in the table.

max() function returns maximum value of specified value in the table.

count() function returns number of rows of the table.

Example:

package net.roseindia.main;

import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class AggregateFunctionHQL {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String minHql = "Select min(emp.salary) FROM Employee emp";
        String maxHql = "Select max(emp.salary) FROM Employee emp";
        String countHql = "Select count(*) FROM Employee emp";

        Query minQuery = session.createQuery(minHql);
        Query maxQuery = session.createQuery(maxHql);
        Query countQuery = session.createQuery(countHql);
        System.out
                .println("Minimum salary in list : " + minQuery.list().get(0));
        System.out
                .println("Maximum salary in list : " + maxQuery.list().get(0));
        System.out.println("Number of Employee : " + countQuery.list().get(0));
    }
}

Output:

Hibernate: select min(employee0_.salary) as col_0_0_ from employee employee0_
Minimum salary in list : 15000
Hibernate: select max(employee0_.salary) as col_0_0_ from employee employee0_
Maximum salary in list : 220000
Hibernate: select count(*) as col_0_0_ from employee employee0_
Number of Employee : 4

Description: Here min(salary) returns the minimum salary in the employee table using HQL.

max(salary) returns the maximum salary in the employee table using HQL.

count(*) returns the number of employee in the table.



September 26, 2012 at 8:56 PM


Suppose that rather than the simple SELECT MAX(emp.salary)... I want the whole Employee object, with the max. salary, how do I write that query?

Something like: SELECT e from Employee e WHERE e.salary = MAX(e.salary) Except that's wrong!



Related Tutorials/Questions & Answers:
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
how to calculate max and min - RUP
and minimum using java language.  Hi friend, In Math class having two function max and min in java. public class MathMaxMin { public static...how to calculate max and min  hye!i'm a new for the java and i want
Advertisements
Count distinct value in HQL.
Count distinct value in HQL.  How do you count distinct value in HQL?   count(distinct fieldName) returns number of distinct value... = sessionFactory.openSession(); String countHql = "Select count(distinct salary
Mysql Min Max
Mysql Min Max       Mysql Min Max is useful to find the minimum and maximum records from a table. Understand with Example The Tutorial illustrate an example from Mysql Min Max
how to calculate max and min in the loop - Java Beginners
how to calculate max and min in the loop  thanks coz giving me... main(String a[]) throws IOException { int max,min; max=Integer.parseInt(a[0]); min=Integer.parseInt(a[0]); for(int i=0;imax) max=Integer.parseInt
hibernate criteria Max Min Average Result Example
hibernate criteria Max Min Average Result Example In this Example, We... the Projections class max, min ,avg methods. Here is the simple Example code files...("Max Salary is : "); System.out.print(row[i]); System.out.printf("\n
SQL Max Count
SQL Max Count       SQL Max Count is used to return the maximum records value of the specified column... 'SQL Max Count'. To elaborate example we create a table 'Stu'. The create table
Hibernate criteria query using Min().
Hibernate criteria query using Min().  How to find minimum value in Hibernate criteria query using Projection?   You can find out minimum value in hibernate criteria by using projection. Projection is an interface
Delete Query using HQL
Delete Query using HQL  Can we write 'AND' with 'WHERE' clause in delete query using HQL? For example: delete from table name where field1 = value1 and field2 = value2   The example query i have written
Hibernate min() Function
Hibernate min() Function This section contains an example of the HQL min() function. min() function in HQL returns the smallest value from the selected... will demonstrate you how to embed the HQL min() function within your code
Hibernate criteria query using Max()
Hibernate criteria query using Max()  What is Hibernate criteria query using Max()?   You can find out maximum value in hibernate criteria by using projection. Projection is an interface and a class
sum and avg function using HQL.
sum and avg function using HQL.  How to use sum and avg function in HQL?   HQL supports many aggregate functions. Sum and avg are some of them. sum() function returns summation of specified field in the table
HQL
HQL  What is the HQL in hibernate ? Explain the use of HQL.   Hi Friend, Please visit on this link....... Hibernate Query Language
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Hibernate HQL query by using like operator.
Hibernate HQL query by using like operator.  How use like operator in HQL?   HQL stands for Hibernate Query Language, provided by Hibernate... = sessionFactory.openSession(); String hql="SELECT emp.name, emp.salary, emp.dateOfJoin
Hibernate max() Function
Hibernate max() Function In this tutorial you will learn about how to use HQL max() function. max() function in HQL returns the largest value from... below will demonstrate you how to embed the HQL max() function within your code
Case-insensitive search using Hibernate Query Language(HQL).
Case-insensitive search using Hibernate Query Language(HQL).  What is Case-insensitive search using Hibernate Query Language
column count using metadata
GET COLUMN COUNT USING "RESULTSETMETADATA"   In this program ,we will get column count using "resultsetmetadata ".Using resultset interface and metadata interface ,we can get properties of column
MySQL Aggregate Function
in the MySQL Query. In this example we use the aggregate function ('COUNT', 'MIN', 'MAX', and 'SUM') in the MySQL Query. The group (aggregate) functions...; SELECT designation, MIN(salary) , MAX(salary) FROM employee GROUP
Finding MAX value from emp_id column using SQL query?
Finding MAX value from emp_id column using SQL query?  I need to select maximum of the Emp_id values from SQL server 2005 table.I was using... of emp id from the table.(Actually it picks up Max value using by looking at 1st
Min Value
Min Value   How to access min value of column from database table... min(id) from QuestionModel result_info"; Query query = session.createQuery(selectQuery); System.out.println("Min Value : " +query.list().get(0
how to count words in string using java
how to count words in string using java  how to count words in string using corejava   Hi Friend, Try the following code: import...: "); String st=input.nextLine(); int count=0
count values using jsp - JSP-Servlet
count values using jsp  hai deepak i have a query i am... and subheadings i want to get the count of textboxes which are heading under that how... by array count can you help me to sort out this problem expecting your favourable
HQL query
HQL query  how to select only password based on username using HQL query from login table where uname and paswrd are two column names
Hibernate count() Function
Hibernate count() Function In this tutorial you will learn how to use the HQL count() function count() function counts the maximum number of rows in a table however it doesn't count the null values of the specified column
Hibernate Min() Function (Aggregate Functions)
(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all... Hibernate Min() Function (Aggregate Functions)  ... the Min() function. Hibernate supports multiple aggregate functions. When
SQL Aggregate Functions List
List Queries. The Aggregate Function include the average, count, min, max, sum etc... | +------+---------+-----------+ 5 rows in set (0.00 sec) COUNT Functions:  The  count (id) Function is a aggregate function that return the sum number of records
HQL in hibernate ? Explain the use of HQL.
HQL in hibernate ? Explain the use of HQL.  What is the HQL in hibernate ? Explain the use of HQL.   HQL stands for Hibernate Query...(); Session session = sessionFactory.openSession(); String hql="SELECT
Hibernate Named HQL using XML Mapping
In this section, you will learn executing Named HQL written in XML Mapping file
Find max and min value from Arraylist
max() and min() respectively. Here is an example that consists of ArrayList of integers and using the max and min methods of Collections class, we have find...Find max and min value from Arraylist In this tutorial, you will learn how
Count Records using the Prepared Statement
Count Records using the Prepared Statement  ... to count all records of the database table by using the PreparedStatement...\PreparedStatement>java CountRecords Count records example using prepared
Generator Tag (Control Tags) Using Count Attributes
Generator Tag (Control Tags) Using Count Attributes   ... tag using the count attribute.  Add the following code snippet...:iterator> tag prints it out using the  <s:property />
Hibernate Max() Function (Aggregate Functions)
(...), sum(...), min(...), max(...) , count(*), count(...), count(distinct... Hibernate Max() Function (Aggregate Functions... to use the Max() function. Hibernate supports multiple aggregate functions. When
Subset Tag (Control Tags) Example Using Count
Subset Tag (Control Tags) Example Using Count       In this section, we are going to describe the subset tag using the count parameter. The count parameter indicates the number of entries
min project with vb - WebSevices
min project with vb  i have do the min project with VB Frantend and oracle backend
HQL Query
HQL Query  Hibernate query language: How to use a left join in Hibernate query language. Can somebody help with an example. I am getting a unexpected token error in HQL Query tool If I use joins
HQL Sample
HQL Sample In this site there are many tutorials and example of HQL. These examples comes with HQL sample code that you can download and run on your computer and learn the HQL by modifying and executing these examples. The HQL Sample
HQL order by.
HQL order by.  HQL Order by query giving problem.   Order... into descending order you can follow the following HQL ? String hql="Select emp FROM...(); String hql="Select emp FROM Employee emp ORDER BY name"; Query query
Rows Count Example Using JDBC ResultSet
Rows Count Example Using  JDBC ResultSet: In this tutorial you can count the number of rows in the database table using ResultSet. Through this example... with count method and execute on created statement object and store result
HQL group By.
HQL group By.  How to use group by in HQL?   In general...=sessionFactory.openSession(); String hql="Select emp.name FROM Employee emp GROUP BY name"; Query query=session.createQuery(hql); query.setResultTransformer
Program to count the number of unique words in a file using HashMap
Program to count the number of unique words in a file using HashMap  import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class CountUniqueWords { public static void main(String args
HQL, Hibernate Query Language
columns. But in case of HQL we write queries using the persistence object... the programmer to write Query using the persistence entity. The HQL is fully... using HQL. HQL allows the developers to write queries in object oriented
javascript max size
javascript max size   How to retrive Max size in JavaScript
Hibernate criteria count.
Hibernate criteria count.  How do we count rows using criteria...(0)); } } Description: Hibernate criteria is used here as alternative to HQL. In general an application has a single sessionFactory. If you are using multiple
Hibernate Count Query
) max( [ distinct | all ] object.property) min( [ distinct | all ] object.property... Hibernate Count Query       In this section we will show you, how to use the Count Query. Hibernate
HQL Query in execute method of Struts2
data from database using hql. And where I need to write the hql query...HQL Query in execute method of Struts2  I am making login page in struts2 and using hibernate . Now I wants to pull user name and password from
Hibernate Query Language (HQL)
Hibernate Query Language (HQL)  What is Hibernate Query Language (HQL)?   HQL is much like SQL and are case-insensitive, except... and execute it against underlying database if HQL is used in the application. HQL
visualization Max-Heap-sort
visualization Max-Heap-sort  To Whom It May Concern, Would you please give the source code of applet Max-heap sort? I want to see the visualization of Max-Heap sort. Best Regards, saeideh
Max size of iPhone application
Max size of iPhone application  Hi, I am developing iPhone application. There are many images and videos. I want to know allowed max size in MB. What is the max size of iphone application? Thanks   Hi, I think
Advertisements
 

 

 

DMCA.com