Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Query query = session.createQuery("from User");
List users = query.list();
for (Iterator iter = users.iterator(); iter.hasNext();) {
User user = (User) iter.next();
System.out.println("user name = "+user.getName());
}
session.getTransaction().commit();
if(session.isOpen()){
session.close();
}