Home Tutorialhelp Comment Hi Harish, You wrap the session.save() method w

 
 

Comment

Deepak Kumar
Hi Harish, You wrap the session.save() method w,
December 9, 2006 at 11:54 PM

Hi Harish,

You wrap the session.save() method within transaction block and then commit the transaction.

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Transaction tx;
try {
tx = session.beginTransaction();
//do some work
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);

tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
session.close();
}

After modifying the code in this way, you will be able to solve the problem.

Thanks
Deepak
View All Comments | View Tutorial
Related Tutorial and Articles

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.