Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // perform database operations tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); e.printStackTrace(); } finally { session.close(); }
Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.eq("firstName", "John")); criteria.addOrder(Order.asc("lastName")); ListThis example shows how to use the Session to create a Criteria object that can be used to execute a query against the database. In this case, the query searches for all employees with a "firstName" of "John" and orders the results by "lastName". Package library: `org.hibernate` is part of the Hibernate Object/Relational Mapping (ORM) framework, which provides an easy and flexible way to map Java objects to relational database tables.employees = criteria.list();