AUTO INCREMENT AND INSERTION OF RECORD IN J2EE USING HIBERNATE FRAMEWORK:
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;
String fname=request.getParameter("fname");
String sname=request.getParameter("sname");
//CREATING SESSION
SessionFactory sdf=new Configuration().configure().buildSessionFactory();
Session s=sdf.openSession();
//TO GET MAX ID
Criteria criteria = s.createCriteria(Datahandler.class).setProjection(Projections.max("id"));
Integer maxid = (Integer)criteria.uniqueResult()+1;
Transaction tr=s.beginTransaction();
//DATA INSERTION
Datahandler obj=new Datahandler(maxid,fname,sname);
//SAVING AND RELEASING RESOURCES
s.save(obj);
tr.commit();
s.close();
out.println("DATA INSERTED....");
No comments:
Post a Comment