// 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;

import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.type.Type;

public class Test {
   private static void delete() {
      try {
         SessionFactory sf = new Configuration().configure(new File("C:\\HibernateConfig.xml")).buildSessionFactory();
         Session s = sf.openSession();
         Transaction tx = s.beginTransaction();
         T1 r = new T1();
         r.setF1(777);
         s.delete(r);
         tx.commit();
         s.close();
      } catch (HibernateException e) {
         e.printStackTrace();
      }
   }
   private static void insert() {
      try {
         SessionFactory sf = new Configuration().configure(new File("C:\\HibernateConfig.xml")).buildSessionFactory();
         Session s = sf.openSession();
         Transaction tx = s.beginTransaction();
         T1 r = new T1();
         r.setF1(777);
         r.setF2("H test");
         s.save(r);
         tx.commit();
         s.close();
      } catch (HibernateException e) {
         e.printStackTrace();
      }
   }
   private static void selectone() {
      try {
         SessionFactory sf = new Configuration().configure(new File("C:\\HibernateConfig.xml")).buildSessionFactory();
         Session s = sf.openSession();
         T1 r = (T1)s.load(T1.class, new Integer(777));
         System.out.println(r.getF1() + " " + r.getF2());
         s.close();
      } catch (HibernateException e) {
         e.printStackTrace();
      }
   }
   private static void selectall() {
      try {
         SessionFactory sf = new Configuration().configure(new File("C:\\HibernateConfig.xml")).buildSessionFactory();
         Session s = sf.openSession();
         List all = s.find("FROM T1 AS t1");
         for(int i = 0; i < all.size(); i++) {
            T1 r = (T1)all.get(i);
            System.out.println(r.getF1() + " " + r.getF2());
         }
         s.close();
      } catch (HibernateException e) {
         e.printStackTrace();
      }
   }
   private static void selectsome() {
      try {
         SessionFactory sf = new Configuration().configure(new File("C:\\HibernateConfig.xml")).buildSessionFactory();
         Session s = sf.openSession();
         List all = s.find("FROM T1 AS t1 WHERE F2 NOT LIKE ?", new Object[] { "Dette%" }, new Type[] { Hibernate.STRING });
         for(int i = 0; i < all.size(); i++) {
            T1 r = (T1)all.get(i);
            System.out.println(r.getF1() + " " + r.getF2());
         }
         s.close();
      } catch (HibernateException e) {
         e.printStackTrace();
      }
   }
   public static void main(String[] args) {
      Logger.getLogger("net.sf.hibernate").setLevel(Level.SEVERE);
      delete();
      insert();
      selectone();  
      selectall();  
      selectsome();  
   }
}
