Criteria API in Hibernate

Criteria API in Hibernate :

The criteria is a simple api for fetching entities by creating Criterion object. With criteria you can apply number of condition with query such as using like wildcard, order by etc.

Here you will see create criteria operation through annotation  in Hibernate. To create criteria you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();

List employees=s.createCriteria(Employee.class).add(Restrictions.like("name", "p%")).addOrder(Order.asc("salary")).list();
t.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

Advertisements

Delete operation in Hibernate

Delete operation in Hibernate :

Here you will see delete operation through annotation  in Hibernate. To create delete operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
Employee emp = (Employee) s.load(Employee.class, 72);
s.delete(emp);
System.out.println("Record deleted successfully!");
t.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

Output :

Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_, employee0_.salary as salary0_0_ from employee employee0_ where employee0_.id=?
Record deleted successfully!
Hibernate: delete from employee where id=?

Update operation in Hibernate

Update operation in Hibernate :

Here you will see update operation through annotation  in Hibernate. To create update operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
Employee e2= (Employee) s.get(Employee.class,72);
e2.setName("Rahul");
s.saveOrUpdate(e2);
System.out.println("Record updated successfully!");
t.commit();


} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}

}
}

Output :

Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_, employee0_.salary as salary0_0_ from employee employee0_ where employee0_.id=?
Record updated successfully!
Hibernate: update employee set name=?, salary=? where id=?

Select operation in Hibernate

Select  operation in Hibernate :

Here you will see select operation through annotation  in Hibernate. To create select operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
List<Employee> employees=s.createQuery("from Employee").list();
System.out.println("Id"+" "+"Name"+" "+"Salary");
System.out.println("----------------------------------");
for(Employee e2:employees) {
System.out.print(e2.getId()+" "+e2.getName()+" "+e2.getSalary()+"\n");
//System.out.print("\n"+e);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

Output :

Hibernate: select employee0_.id as id0_, employee0_.name as name0_, employee0_.salary as salary0_ from employee employee0_
Id Name Salary
———————————-
72 Rahul 35000
73 Pradeep Yadav 45000
74 Amit 65000

BUILD SUCCESSFUL (total time: 3 seconds)

Insert operation in Hibernate

Insert operation in Hibernate :

Here you will see insert operation through annotation  in Hibernate. To create insert operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
Employee e = new Employee();
e.setName("Pradeep Yadav");
e.setSalary(45000);
s.save(e);
System.out.println("Record inserted successfully!");
t.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
s.close();
}
}
}

Output :

Hibernate: insert into employee (name, salary) values (?, ?)
Record inserted successfully!

 

Advantages of Hibernate Framework

Advantages of  Hibernate Framework :

  • Hibernate has capability to generate primary keys automatically while storing the records into database.
  • Hibernate provide automatic table creation facility.
  • Hibernate framework supports Inheritance, Associations, Collections.
  • Hibernate has annotations facilities.
  • In hibernate if you save the subclass class object, then its super class object will also be stored into the database, that is hibernate supporting inheritance.
  • Hibernate provide caching technique through this, an application can increase its performance automatically.
  • Hibernate supports relationships such as One-To-Many,One-To-One, Many-To-Many-to-Many, Many-To-One
    it is also supports collections like list, set, map, bag.
  • In hibernate you only have Un-checked(Runtime) exceptions, so don’t need to write try-catch block, or throws. Because in hibernate has translator which converts checked to Un-checked.
  • Hibernate has its own query language, that is hibernate query language(HQL) which is database independent. So that if you will change the database, then your application will works properly due to HQL is database independent.
  • Pagination can use in hibernate is very simple.
  • Hibernate provides Dialect classes, so you don’t need to write sql specific queries in hibernate, instead use the methods provided by API.

What is Hibernate Framework

Hibernate Framework :

Hibernate framework is an ORM(Object Relational Mapping) tools which is used to map java class with database table name and class variable with database table’s column. It is open source(free of cost) anybody can download its jar from hibernate.org/orm/downloads/ and use.

Hibernate framework provide database specific Dialect which used to switch different database without code change in application. Through this a programmer don’t need to write database specific query.