Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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:updateorg.hibernate.dialect.Oracle9Dialectjdbc:oracle:thin:@localhost:1521:xesystemoracleoracle.jdbc.driver.OracleDriverclass="com.javaspot.Employee"/>
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 QueryQuery query = session.getNamedQuery("findEmployeeByName");query.setString("name", "amit");List employees=query.list();Iterator 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:
<class name="com.javatpoint.Employee" table="em">class="native">class>
The persistent class:
Employee.javapackage com.javaspot;public class Employee {int id;String name;int salary;String job;//getters and setters}
hibernate.cfg.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.