Pages

Friday, July 15, 2011

Java Hibernate code

 Hibernate sample code using MySQL

public class FirstExample {
    public static void main(String[] args) {
        Session session = null;

        try{
       
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

             Contact contact = new Contact();
            contact.setId(0);
            contact.setFirstName("Deepak");
            contact.setLastName("Kumar");
            contact.setEmail("deepak_38@yahoo.com");

            session =sessionFactory.openSession();
         Transaction tx=        session.beginTransaction();
        session.save(contact);
        tx.commit();

            session.flush();
            session.close();

        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            // Actual contact insertion will happen at this step
            session.flush();
            session.close();
        }
    }
}
----------------------------

No comments:

Post a Comment