Criteria API example

Criteria API is yet another way of retrieving values from database provided by hibernate. Using HQL has some disadvantages. If we write complex queries using HQL, at some point of time these queries look much similar to our traditional sql queries. To overcome with this issue, hibernate has provided us with the criteria api. Criteria is a simplified API for retrieving by composing criterion objects. This is very convenient approach for functionality like "search" screens where there is a variable number for conditions to be placed upon the result search.Below is a simple example of how to use a criteria API.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">hr</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.ram.dao.Jobs"/>

    </session-factory>

</hibernate-configuration>
File: com.ram.dao.Jobs.java
package com.ram.dao;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Jobs {
 @Id
 private String job_id;
 private String job_title;
 private int min_salary;
 private int max_salary;
 
 public String getJob_id() {
  return job_id;
 }
 public void setJob_id(String job_id) {
  this.job_id = job_id;
 }
 public String getJob_title() {
  return job_title;
 }
 public void setJob_title(String job_title) {
  this.job_title = job_title;
 }
 public int getMin_salary() {
  return min_salary;
 }
 public void setMin_salary(int min_salary) {
  this.min_salary = min_salary;
 }
 public int getMax_salary() {
  return max_salary;
 }
 public void setMax_salary(int max_salary) {
  this.max_salary = max_salary;
 }
}

File: com.ram.hibernate.CriteriaAPIExample.java
package com.ram.hibernate;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.ram.dao.Jobs;

public class CriteriaAPIExample {
 public static void main(String[] args) {
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  session.beginTransaction();
  
  Criteria criteria = session.createCriteria(Jobs.class);
  criteria.add(Restrictions.eq("job_id", "MK_MAN"));
  
  List jobs = (List) criteria.list();
  
  for(Jobs job: jobs){
   System.out.println("Jon Id: "+job.getJob_id());
   System.out.println("Job Title "+job.getJob_title());
   System.out.println("Minimum Salary "+job.getMin_salary());
   System.out.println("Maximum Salary "+job.getMax_salary());
  }
  
  session.getTransaction().commit();
  session.close();
 }
}

Execute CriteriaAPIExample.java and you get the output as shown below.

No comments:

Post a Comment