Home Struts Struts Struts2.2.1 Hibernate criteria example using struts2

Ask Questions?

View Latest Questions


Hibernate criteria example using struts2
Posted on: March 31, 2011 at 12:00 AM
Hibernate criteria example using struts2

Hibernate criteria example using struts2

This hibernate criteria example using struts2 is used for defining the basics of criteria. The hibernate criteria API is used for creating dynamic query. With the help of criteria, you can create query without HQL.

In this hibernate criteria example using struts2, we will see how to access records from database using criteria.

Syntax of criteria: 

Criteria criteria = session.createCriteria(StudentModel.class)




<meta http-equiv="refresh" content="1;URL='studentinfo'">

<title>Insert title here</title>



<h2>:::::::::::::::::Student details ::::::::::::</h2>





<?xml version="1.0" encoding="UTF-8"?>


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"



<constant name="struts.devMode" value="false" />

<package name="roseidia" extends="struts-default">

<action name="studentinfo" class="main.roseindia.action.StudentInfoAction">





StudentInfoAction.java(Action Class)

package main.roseindia.action;

import java.util.List;

import main.roseindia.dao.StudentDAO;

import main.roseindia.model.StudentModel;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

public class StudentInfoAction extends ActionSupport implements ModelDriven{

StudentModel obModel;

StudentDAO obDao=new StudentDAO();

List studentModels;


public String execute() throws Exception {

this.studentModels= (List) obDao.listCriteria();

return SUCCESS;



public Object getModel() {

obModel=new StudentModel();

return obModel;


public List getStudentModels() {

return studentModels;


public void setStudentModels(List studentModels) {

this.studentModels = studentModels;




package main.roseindia.dao;

import java.util.List;

import org.hibernate.Criteria;

import org.hibernate.Session;

import main.roseindia.HibernateUtil.HibernateUtil;

import main.roseindia.model.StudentModel;

public class StudentDAO {

StudentModel obStudentModel = new StudentModel();

Session session = null;

public List<StudentModel> listCriteria() {

session = HibernateUtil.getSessionfactory().getCurrentSession();


List<StudentModel> list = null;

Criteria criteria = session.createCriteria(StudentModel.class);

list = criteria.list();

return list;



HibernateUtil.java(Action Class)

package main.roseindia.HibernateUtil;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

private static SessionFactory sessionFactory;

static {

try {

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

} catch (Exception e) {





public static SessionFactory getSessionfactory() {

return sessionFactory;



StudentModel.java(Action Class)

package main.roseindia.model;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;



public class StudentModel {

private int roll;

private String fname;

private String lname;

private String sclass;

private String saddress;

private String semail;

private int password;




public int getRoll() {

return roll;



public String getFname() {

return fname;



public String getLname() {

return lname;



public String getSclass() {

return sclass;



public String getSaddress() {

return saddress;



public String getSemail() {

return semail;



public int getPassword() {

return password;


public void setRoll(int roll) {

this.roll = roll;


public void setFname(String fname) {

this.fname = fname;


public void setLname(String lname) {

this.lname = lname;


public void setSclass(String sclass) {

this.sclass = sclass;


public void setSaddress(String saddress) {

this.saddress = saddress;


public void setSemail(String semail) {

this.semail = semail;


public void setPassword(int password) {

this.password = password;




<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"




<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.connection.pool_size">1</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

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

<property name="current_session_context_class">thread</property>

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

<mapping class="main.roseindia.model.StudentModel" />




Download this code..

Related Tags for Hibernate criteria example using struts2: