HIbernate CRUD operations

Below are some of the DB operations performed in Hibernate .


First we need to get the SessionFactory  object . We can get the object like this
SessionFactory factory= new AnnotationConfiguration().configure(hibernate.cfg.xml).buildSessionFactory();


1) Insert:
Session session=factory.openSession();
Employee emp=new Employee("empID","empName");
session.save(emp);

2)Delete:
Session session=factory.openSession();
Employee emp=session.createCriteria(Employee.class).add(Restrictions.eq("empID","123")).uniqueResult();
session.delete(emp);

3)Update:
Criteria criteria = session.createCriteria(Employee.class).add(Restrictionss.eq("id", 1));
Employee emp = (Employee) criteria.uniqueResult();
emp.setSalary(5000);
session.update(emp);

4)Select:
Session session=factory.openSession();
List empList=session.createCriteria(Employee.class).add(Restrictions.eq("company","Google")).list(); // Will get the list of employees.

So these were the basic operations you can use in Hibernate :)




Brother website for healthcare : http://delighthealthcare.in/



Comments

Popular posts from this blog

Arrays.sort() and Collections.sort() NullPointerException

org.xml.sax. SAXParseException: Element type "web-app" must be declared.

Project facet Java 6.0 is not supported by target runtime JBoss