Hibernate Criteria Query

Click to rate this post!
[Total: 0 Average: 0]
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
  • oracle 12c rac | OnlineITGuru
    Oracle RAC Training
  • Oracle is the large vendor in providing the various storge services to the people across the globe. This vendor provides a different amount of storage services to the people across the globe.

  • salesforce lightning training | OnlineITGuru
    Salesforce Lightning Training
  • Developing an application is not a simple and easy task. There are various parameters that the web developer need to take care while developing an application. One of those parameters that the developer needs to take care of is the code reusability.

  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

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

Call Now Button