Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Criteria in Hibernate:-

Criteria return complete objects after data selection, it is the predefined interface of Hibernate which contains multiple methods to perform the conditional operation.

For example, if we want to select the record of students whose fees are less than 50000 then we will use Criteria Object. It is the best way to select data from the database.

The main difference between criteria and HQL is that criteria return complete objects and HQL return partial objects.

We can perform data insertion, update, deletion, and selection using HQL but using Criteria we can select data only.

Syntax of Criteria in Hibernate?

Criteria q = session.createCriteria(PojoClassname);  //Vendor.class
List lst = q.list();
Iterator it = lst.iterator();
    PoJO v=(POJO);
   out.print(v.getterOfPojo()+" "+v.getterOfPojo()+"<br>");

What are Restrictions?

It is another interface that is used to apply condition on Criteria Object, Restrictions Interface provide predefine method to apply the condition. It returns Criterion type data hence we always get the filtered data using Criterion reference.

Criterion c ="columnname",new Integer(value));

Now I am providing a complete example of Criteria under the JSP Web Page:-

 <%@page import="org.hibernate.criterion.*"%>
<%@page import="org.hibernate.cfg.*"%>
<%@page import="org.hibernate.*"%>
<%@page import="java.util.*" %>
<%@page import="dal.Reg" %>
<%@page import="helper.Datahelper" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<table border="1">
Configuration cfg = new Configuration();
SessionFactory factory = cfg.buildSessionFactory();
Session sess = factory.openSession();
Criteria crt = sess.createCriteria(Reg.class);
//Criterion ct = Restrictions.eq("username","s");
//Criterion ct ="username","k%");
ArrayList arr = new ArrayList();
Criterion ct ="password",arr);
List lst = crt.list();
Iterator it = lst.iterator();
Reg r =(Reg);
   <tr><td><%= r.getUsername() %></td><td><%= r.getPassword()  %></td><td><%= r.getEmail() %> </td><td><%= r.getMobile() %></td><td><a href="Find.jsp?q=<%= r.getUsername() %>">Edit</a></td><td><a href="Deleteconfirm.jsp?q=<%= r.getUsername() %>">Delete</a></td></tr>

Complete Example of Criteria:-

package mappingser;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import mapping.*;
 * Servlet implementation class ManyToOneSer
public class ManyToOneSer extends HttpServlet {
private static final long serialVersionUID = 1L;
     * @see HttpServlet#HttpServlet()
    public ManyToOneSer() {
        // TODO Auto-generated constructor stub

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Configuration cfg = new Configuration();

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
   Criteria q = session.createCriteria(Vendor.class);
Criterion c ="vendroid",new Integer(102));
List lst = q.list();
Iterator it = lst.iterator();
Vendor v=(Vendor);
    out.print(v.getVendroid()+" "+v.getVerndroname()+"<br>");
    out.println("One To Many is Done..!!");


* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post