Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.Date;
- import java.util.Iterator;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class ManageEmployee {
- private static SessionFactory factory;
- public static void main(String[] args) {
- try {
- factory = new Configuration().configure().buildSessionFactory();
- } catch (Throwable ex) {
- System.err.println("Failed to create sessionFactory object." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- ManageEmployee ME = new ManageEmployee();
- /* Add few employee records in database */
- ME.addStudent("Zara", 001, 25);
- ME.addStudent("Daisy", 002, 30);
- ME.addStudent("John", 003, 19);
- }
- /* Method to CREATE an employee in the database */
- public void addStudent(String name, int number, int age){
- Session session = factory.openSession();
- Transaction tx = null;
- Integer employeeID = null;
- try {
- tx = session.beginTransaction();
- Student employee = new Student(name, number, age);
- employeeID = (Integer) session.save(employee);
- tx.commit();
- } catch (HibernateException e) {
- if (tx!=null) tx.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement