Studyon Minte9.com
Hibernate




Example (list)



(employee)
------------------------------------
first_name |   last_name 
------------------------------------
ion	        |   popescu
andrei       |   marian
-----------------------------------


package testexample; import java.util.List; import java.util.Iterator; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.HibernateException; public class Testexample { private static SessionFactory factory; public static void main(String args[]) { factory = new Configuration().configure().buildSessionFactory(); Testexample app = new Testexample(); app.listEmployees(); } public void listEmployees() { Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); List employees = session.createQuery("FROM Employee").list(); Iterator it = employees.iterator(); while (it.hasNext()) { Employee em = (Employee) it.next(); System.out.println(em.getFirstName() + " " + em.getLastName()); // ion popescu } } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } } }
 


http://www.tutorialspoint.com/hibernate/hibernate_examples.htm