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.

Example:

@NamedQueries(

{

@NamedQuery(

name = “findEmployeeByName”,

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

)

}

)

Example:
Employee.java:

package com.javaspot;

import javax.persistence.*;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

@NamedQueries(

{

@NamedQuery(

name = “findEmployeeByName”,

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

)

}

)

@Entity

@Table(name=”em”)

 public class Employee {

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

int id;

String name;

int salary;

String job;

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

//getters and setters

}

hibernate.cfg.xml:

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

<!DOCTYPE hibernate-configuration PUBLIC

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

“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>

<session-factory>

<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”/>

</session-factory>

</hibernate-configuration>

FetchData.java:

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();

configuration.configure(“hibernate.cfg.xml”);

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();

while(itr.hasNext()){

Employee e=itr.next();

System.out.println(e);

}

session.close();

}

}

Hibernate Named Query by mapping file
emp.hbm.xml:

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

<!DOCTYPE hibernate-mapping PUBLIC

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

“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

<hibernate-mapping>

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

<id name=”id”>

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

</id>

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

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

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

</class>

<query name=”findEmployeeByName”>

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

</query>

</hibernate-mapping>

The persistent class:

Employee.java

package com.javaspot;

public class Employee {

int id;

String name;

int salary;

String job;

//getters and setters

}

hibernate.cfg.xml:

<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.

Drop Us A Query

Trending Courses
  • Python and Django 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 Online 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.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification 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).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


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