criteria in hibernate

Q. What is criteria in hibernate? Write an example of criteria using annotation?

Steps to run below example –

Step 1 – Create a java project(Since we are not using maven for jar/library, need to download the jar and add manually in the project)

Step 2 – Download hibernate jar and add it to project( right-click on project/properties/java build path/libraries/add external jars/ browse and add).

Step 3 – define the below classes and run the main class(Don’t forget to change the username and password in hibernate.cfg.xml for the database, that you have given during installation of Oracle in your machine ).

 

Let’s see simple example for criteria –

Book.java – 

package hibernate.criteriaquery;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

@Entity
public class Book {
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int bookId;
	
	@Column(name = "book_name")
	private String bookName;

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	
}

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="connection.driver_class">oracle.jdbc.driver.OracleDriver
</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="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>

 <mapping class="hibernate.criteriaquery.Book" />

 
</session-factory>

</hibernate-configuration>

CriteriaQueryExample.java – 

package hibernate.criteriaquery;

import java.util.List;

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.criterion.Restrictions;

public class CriteriaQueryExample {
	
	public static void main(String[] args) {
		
		SessionFactory factory = null;
		
		Session session = null;
		
		Configuration configuration = new Configuration().configure();
		
		try {
			
			factory = configuration.buildSessionFactory();
			
			session = factory.openSession();
			
			Transaction tx = session.beginTransaction();
			
			Criteria criteria=session.createCriteria(Book.class);
			
			System.out.println("fetching record on basis of bookname");
			
			criteria.add(Restrictions.eq("bookName","of coure i love you"));
			
			List<Book> bookList = criteria.list();
			
			for(Book bl: bookList) {
				
				System.out.println("id  "+bl.getBookId());
				
				System.out.println("name  "+bl.getBookName());
				
			}
			
			System.out.println("fetching record on basis of bookId");
			
			//Criteria criteria1=session.createCriteria(Book.class);
			criteria.add(Restrictions.eq("bookId",6));
			
			List<Book> bookList1 = criteria.list();
			
			System.out.println("size of list--  "+bookList1.size());
			
			for(Book b2: bookList1) {
				
				System.out.println("id  "+b2.getBookId());
				
				System.out.println("name  "+b2.getBookName());
			}
			
			tx.commit();
			
			System.out.println("exam usingple of named query using annnotation");
		} catch (Exception e) {
			
			e.printStackTrace();
			

		} finally {

			 rsession.close();
			
			factory.close();
		}
	}
}

When you run this program expected output is –