0% found this document useful (0 votes)
80 views3 pages

Sample Appliction For Hibernate Criteria Query

This document provides code samples for creating Hibernate criteria queries to retrieve data from a database. It creates a StudentCriteria DAO class with methods to get all students or students by a specific course. A CriteriaTester class calls these methods and outputs the results. The code initializes a SessionFactory, builds CriteriaQuery objects to define queries, executes the queries, and returns lists of student objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Sample Appliction For Hibernate Criteria Query

This document provides code samples for creating Hibernate criteria queries to retrieve data from a database. It creates a StudentCriteria DAO class with methods to get all students or students by a specific course. A CriteriaTester class calls these methods and outputs the results. The code initializes a SessionFactory, builds CriteriaQuery objects to define queries, executes the queries, and returns lists of student objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sample Appliction for Hibernate Criteria Query

Let us learn to create some Queries to retrive data from the database.

1. Let us take the Annotation demo project


2. For querying data let us create a separate dao class under a new package called com.wipro.dao
a. Create a package called com.wipro.dao
b. Under the package create class called StudentCriteria

c. Edit the class to add the following script

package com.wipro.dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import com.wipro.bean.Student;

public class StudentCriteria {


SessionFactory factory;
/**
* Default Constructor
* It initializes the SessionFactory based on configuration details
* provided from the myconfig.cfg.xml file
*/
public StudentCriteria() {
Configuration cfg = new
Configuration().configure("myconfig.cfg.xml");
factory = cfg.buildSessionFactory();
}

public List<Student> getAllStudents(){


Session session = factory.openSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Student> cr = cb.createQuery(Student.class);
Root<Student> root = cr.from(Student.class);
cr.select(root);

Sensitivity: Internal & Restricted


Query<Student> query = session.createQuery(cr);
return query.list();
}

public List<Student> getStudentsByCourse(String course){


Session session = factory.openSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Student> cr = cb.createQuery(Student.class);
Root<Student> root = cr.from(Student.class);
cr.select(root);
Predicate predicate = cb.equal(root.get("course"), course);
cr.where(predicate);
Query<Student> query = session.createQuery(cr);
return query.list();
}

d. Create the Tester class called CriteriaTester under service package

e. Edit the code with following script

package com.wipro.service;

import java.util.List;

import com.wipro.bean.Student;
import com.wipro.dao.StudentCriteria;
import com.wipro.dao.StudentDAO;

public class CriteriaTester {

public static void main(String[] args) {


StudentCriteria dao = new StudentCriteria();

/**
* Display all the records
*/
List<Student> result = dao.getAllStudents();
System.out.println("Student Records");
for(Student ob : result)
System.out.println(ob);

System.out.println("****************************************************
**");

/**
* Display Only Oracle the records

Sensitivity: Internal & Restricted


*/
List<Student> result1 = dao.getStudentsByCourse("Oracle");
System.out.println("Student Records");
for(Student ob : result1)
System.out.println(ob);

System.out.println("****************************************************
**");

}
}

f. Run and verify the output

Sensitivity: Internal & Restricted

You might also like