Solution for Insert Record problem in hibernate Eg,
February 27, 2008 at 11:44 AM
At the time of working with this example: - table gets created in database - but no record is inserted
The given below is the solution for the Porblem:
In your FirstExample.java, the transaction should be started explicitly and after saveing your contact details transaction should be commited explicitly.
write below lines before the doing anything with your contact class. ----------------------------------------------- tx = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object System.out.println("Creating New Contact detail."); Contact contact = new Contact(); System.out.println("providing contact details"); contact.setId(6); // Primary key has to be unique for every record inserted! contact.setFirstName("subnesh"); contact.setLastName("sharma"); contact.setEmail("subnesh@gmail.com"); session.save(contact); tx.commit(); System.out.println("Contact saved into database successfully"); ------------------------------------------------
or
rather then making changes in your FirstExample.java (use same code as privided in this tutorial), you can add new property to hibernate.cfg.xml file with below code: