Search This Blog

 Criteria in Hibernate:-

Criteria return complete object for data selection, it is the predefined interface of java which contain multiple methods to perform the operation.


It is the best way to select data from the database .


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



Complete Example of Criteria:-

package mappingser;

import java.io.IOException;
import java.io.PrintWriter;
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
 */
@WebServlet("/ManyToOneSer")
public class ManyToOneSer extends HttpServlet {
private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManyToOneSer() {
        super();
        // 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();
response.setContentType("text/html");
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();

/* Vendor v =new Vendor();

            v.setVendroid(103);
            v.setVerndroname("xyz");

//Customer c1=new Customer();
   // c1.setCustomerid(504);
  // c1.setCustomername("customer4");
    //c1.setParentObjects(v);


          /*  Customer c2=new Customer();

c2.setCustomerid(505);
         c2.setCustomername("customer5");
         c2.setParentObjects(v); */


     

   // Transaction tx = session.beginTransaction();

                    //  session.save(v);

                    //  session.save(c2);

                 


//    tx.commit();*/

//Query q = session.createQuery("select v.vendroid,v.verndroname,c.customername from Customer c  Inner Join c.parentObjects v");

Criteria q = session.createCriteria(Vendor.class);
Criterion c = Restrictions.gt("vendroid",new Integer(102));
q.add(c);
List lst = q.list();
Iterator it = lst.iterator();
while(it.hasNext())
{
Vendor v=(Vendor)it.next();
    out.print(v.getVendroid()+" "+v.getVerndroname()+"<br>");
}
session.close();
    out.println("One To Many is Done..!!");
    factory.close();

}

/**
* @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