// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test.hibshard.common;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;

public abstract class BaseTest {
	protected abstract SessionFactory getSessionFactory();
	public void execute() {
        SessionFactory sf = getSessionFactory();
        Session s = sf.openSession();
        // get France
        System.out.println("get France");
        Country fr = (Country) s.load(Country.class, "France");
        System.out.println(fr);
        // get all
        System.out.println("get all");
        @SuppressWarnings("unchecked")
		List<Country> all = s.createQuery("SELECT c FROM Country AS c").list() ;
        for(Country c : all) {
            System.out.println(c);
        }
        // get Europe
        System.out.println("get Europe");
        Query q = s.createQuery("SELECT c FROM Country AS c WHERE continent = :continent");
        q.setParameter("continent", "Europe", StandardBasicTypes.STRING);
        @SuppressWarnings("unchecked")
		List<Country> alleu = q.list() ;
        for(Country c : alleu) {
            System.out.println(c);
        }
        // get North America
        System.out.println("get North America");
        @SuppressWarnings("unchecked")
		List<Country> allna = s.createCriteria(Country.class).add(Restrictions.eq("continent", "North America")).list() ;
        for(Country c : allna) {
            System.out.println(c);
        }
        // add Mexico City
        System.out.println("add Mexico");
        Transaction trans = s.beginTransaction();
        Country mx = new Country("Mexico", "North America", "Mexico City");
        s.save(mx);
        trans.commit();
        // get all
        System.out.println("get all");
        @SuppressWarnings("unchecked")
		List<Country> allagain = s.createQuery("SELECT c FROM Country AS c").list() ;
        for(Country c : allagain) {
            System.out.println(c);
        }
        s.close();
	}
}
