Hibernate Criteria Query

Hibernate Criteria Query Language:

The Hibernate Criteria Query Language (HCQL) is utilized to bring the records in view of the particular criteria. The Criteria interface gives techniques to apply criteria, for example, retrieving every one of the records of table whose compensation is more noteworthy than 50000.

Advantage of HCQL

The HCQL gives techniques to include criteria, so it is simple for the java developer to include criteria. The java developer can include numerous criteria a question.

Criteria Interface

The Criteria interface gives numerous techniques to indicate criteria. The question of Criteria can be acquired by calling the createCriteria() strategy for Session interface.

Syntax :

public Criteria createCriteria(Class c)

  1. public Criteria add(Criterion c) is used to add restrictions.
  2. public Criteria addOrder(Order o)
  3. public Criteria setFirstResult(int firstResult)
  4. public Criteria setMaxResult(int totalResult)
  5. public List list()
  6. public Criteria setProjection(Projection projection)

Restrictions class:

The Order class speaks to a request. The ordinarily utilized techniques for Restrictions class are as per the following

  1. public static SimpleExpression lt(String propertyName,Object value)
  2.  public static SimpleExpression le(String propertyName,Object value)
  3.  public static SimpleExpression gt(String propertyName,Object value)
  4.  public static SimpleExpression ge(String propertyName,Object value)
  5.  public static SimpleExpression ne(String propertyName,Object value)
  6.  public static SimpleExpression eq(String propertyName,Object value)
  7.  public static Criterion between(String propertyName, Object low, Object high)
  8.  public static SimpleExpression like(String propertyName, Object value)
Order class:
  1. public static Order asc(String propertyName)
  2.  public static Order desc(String propertyName)
 Hibernate NamedQuery:

The hibernate named inquiry is approach to utilize any question by some important name. It resembles utilizing alias names. The Hibernate structure gives the idea of named inquiries so application  programmer require not to dissipate questions to all the java code.

  • by annotation
  • by mapping file.
Named Query by annotation:

In the event that you need to utilize named question in rest, you need learning of @NamedQueries and @NamedQuery comments.

@NameQueries comment is utilized to characterize the different named inquiries.

@NameQuery comment is utilized to characterize the single named question.





name = “findEmployeeByName”,

query = “from Employee e where e.name = :name”





package com.javaspot;

import javax.persistence.*;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;




name = “findEmployeeByName”,

query = “from Employee e where e.name = :name”






 public class Employee {

public String toString(){return id+” “+name+” “+salary+” “+job;}

int id;

String name;

int salary;

String job;



//getters and setters



<?xml version=’1.0′ encoding=’UTF-8′?>

<!DOCTYPE hibernate-configuration PUBLIC

“-//Hibernate/Hibernate Configuration DTD 3.0//EN”




<property name=”hbm2ddl.auto”>update</property>

<property name=”dialect”>org.hibernate.dialect.Oracle9Dialect</property>

<property name=”connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>

<property name=”connection.username”>system</property>

<property name=”connection.password”>oracle</property>

<property name=”connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>

<mapping class=”com.javaspot.Employee”/>




package com.javaspot;

import java.util.Iterator;

import java.util.List;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.*;

public class FetchData {

public static void main(String[] args) {

AnnotationConfiguration configuration=new AnnotationConfiguration();


SessionFactory sFactory=configuration.buildSessionFactory();

Session session=sFactory.openSession();

//Hibernate Named Query

Query query = session.getNamedQuery(“findEmployeeByName”);

query.setString(“name”, “amit”);

List<Employee> employees=query.list();

Iterator<Employee> itr=employees.iterator();


Employee e=itr.next();






Hibernate Named Query by mapping file

<?xml version=’1.0′ encoding=’UTF-8′?>

<!DOCTYPE hibernate-mapping PUBLIC

“-//Hibernate/Hibernate Mapping DTD 3.0//EN”



<class name=”com.javatpoint.Employee” table=”em”>

<id name=”id”>

<generator class=”native”></generator>


<property name=”name”></property>

<property name=”job”></property>

<property name=”salary”></property>


<query name=”findEmployeeByName”>

<![CDATA[from Employee e where e.name = :name]]>



The persistent class:


package com.javaspot;

public class Employee {

int id;

String name;

int salary;

String job;

//getters and setters



<mapping resource=”emp.hbm.xml”/>

Caching in Hibernate:

Hibernate storing enhances the execution of the application by pooling the question in the reserve. There are for the most part two sorts of storing first level reserve and second level reserve.

First Level Cache:

Session question holds the principal level reserve information. It is empowered as a matter of course. The principal level store information won’t be accessible to whole application. An application can utilize numerous session protest.

Second Level Cache:

Session Factory protest holds the second level store information. The information put away in the second level store will be accessible to whole application. Be that as it may, we have to empower it explicitly. Second Level Cache executions are given by various sellers.

to our newsletter

Drop Us A Query

Trending Courses
  • Pega testing training
    PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • blockchain course
    Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • ethical hacking course
    Ethical Hacking Certification Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • python training
    Python Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Training
    Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • what is pega
    PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

100% Secure Payments. All major credit & debit cards accepted.