I created a simple hibernate project where I am inserting a row into database table. But it insert the data only when I use org.hibernate.Transaction commit. If I dont use transaction commit It neither insert any thing in to database nor show any error on console.
package code;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;
org.hibernate.Transaction tx = null;
try{
// This step will read hibernate.cfg.xml
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set
tx = session.beginTransaction();
System.out.println("Inserting Record");
Contact contact = new Contact();
//contact.setId(67);
contact.setFirstname("Deepak");
contact.setLastname("Kumar");
contact.setEmail("
[email protected]");
session.save(contact);
System.out.println(contact.getId());
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
tx.commit();
session.flush();
session.close();
}
}
}
Please tell me what may be the reason.