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)
index.jsp
<html> <head> <meta http-equiv="refresh" content="1;URL='studentinfo'"> <title>Insert title here</title> </head> <body> <h2>:::::::::::::::::Student details ::::::::::::</h2> Loading............... </body> </html> |
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="false" /> <package name="roseidia" extends="struts-default"> <action name="studentinfo" class="main.roseindia.action.StudentInfoAction"> <result>jsp/showInfo.jsp</result> </action> </package> </struts> |
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; @Override public String execute() throws Exception { this.studentModels= (List) obDao.listCriteria(); return SUCCESS; } @Override public Object getModel() { obModel=new StudentModel(); return obModel; } public List getStudentModels() { return studentModels; } public void setStudentModels(List studentModels) { this.studentModels = studentModels; } } |
StudentDAO.java(DAO)
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(); session.beginTransaction(); 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) { e.getMessage(); e.toString(); } } 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; @Entity @Table(name="studentinfo") public class StudentModel { private int roll; private String fname; private String lname; private String sclass; private String saddress; private String semail; private int password; @Id @GeneratedValue @Column(name="id") public int getRoll() { return roll; } @Column(name="fname") public String getFname() { return fname; } @Column(name="lname") public String getLname() { return lname; } @Column(name="sclass") public String getSclass() { return sclass; } @Column(name="address") public String getSaddress() { return saddress; } @Column(name="email") public String getSemail() { return semail; } @Column(name="password") 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; } } |
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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/criteriaappl</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" /> </session-factory> </hibernate-configuration> |
Advertisements
Ads
Ads