http://www.roseindia.net/hibernate/firstexample.shtml
http://www.roseindia.net/hibernate/runninge-xample.shtml
with little modification of code as below:
public static void main(String[] args) {
Session session = null;
Transaction t = null;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
t = session.beginTransaction();
session.beginTransaction();
Contact contact = new Contact();
contact.setId(0);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
t.commit();
System.out.println("Done");
}catch(Exception e){
t.rollback();
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
we need to begin and commit transaction to push the data into table.
No comments:
Post a Comment