package net.roseindia.hqlexamples;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import net.roseindia.model.*;

/**
 * HQL Select Example In this example we are selecting all the customers from
 * the datastore
 */

public class HQLSelect {

	public static void main(String[] args) throws Exception {
		/** Getting the Session Factory and session */
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.getCurrentSession();
		/** Starting the Transaction */
		Transaction tx = session.beginTransaction();

		// Create Select Clause HQL
		String HQL_QUERY = "select c from Customer c";
		Query query = session.createQuery(HQL_QUERY);
		List result = query.list();
		for (Iterator it = result.iterator(); it.hasNext();) {
			Customer customer = (Customer) it.next();
			System.out.println("ID: " + customer.getId());
			System.out.println("Name: " + customer.getCustomerName());
			System.out.println("Phone: " + customer.getCustomerPhone());
		}

		/** Closing Session */
		session.close();
	}
}