package com.hibernate.example;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TestEmployee {
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure();
config.addAnnotatedClass(Employee.class);
new SchemaExport(config).create(true,true);
Employee emp1 = new Employee();
emp1.setEmpname("Umair");
Employee emp2 = new Employee();
emp2.setEmpname("Baig");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
Query query = session.createQuery("From Employee");
List emp = query.list();
for(int i=0;i<emp.size();i++){
Employee empUser = (Employee) emp.get(i);
System.out.println("Employee Name : "+empUser.getEmpname()+"\n");
}
session.close();
}
}