Hibernate: new insert always overrides previous data
Nina Anderson
Ranch Hand
Joined: Jul 18, 2006
Posts: 148
posted
0
Hey guys
I'm new to using Hibernate.
My application is trying to INSERT data into my "Country" database table. Each time I execute the application, the insert is successful. However, the new data overrides the previous data and I'm wondering why a new record is not created each time I do an insert.
////BEGIN: This is a test to add data in the database
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object Country country = new Country(); country.setCountryID(258); //I change this value each time country.setCountryCode("183"); //I change this value each time country.setCountryDesc("Test Nina country3"); //I change this value each time //country.setFirstName((String)contactForm.get("firstName")); session.save(country);
// Actual country insertion will happen at this step session.flush(); session.connection().commit(); session.close();
1) what about the values inside your database. do they change and does the id change appropriately to your new source-code ID after each new insert? if not: maybe you forgot to deploy the application on your tomcat/app-server if yes: have a look at 2)
2) maybe you somehow are dropping and re-creating tables after every new compile and restart of your webapp, so the tables are initially empty.
in general this is not a very good way to test your app, by changing values inside source code. believe it is a pain. invest some effort into testing and build harness and you will see how easy live is. remember that doing things manually which can be automated end in many mistakes and takes much longer. computers love to help you with that ;)