Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New record/row is not getting inserted.Existing record/row is getting updated through hibernate

 
ashwin bala
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am practicing struts with hibernate.I am trying to inserting records through a JSP form.When there is no record in DB one new row is getting
inserted but after that when ever i inserted all the previous are getting removed and only the last record what i inserted is available in database.

Please help me out in this.Should I change anything the hibernate config file ?If required I will put the entire code.Thanks in advance.

regards
Ashwin
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's quite likely that you will have to change something in the configuration, yes. One possible reason for the situation you describe is that you always use the same value for the primary key for all objects. Which might be a configuration problem or a coding problem. And I expect there are other possible reasons.

Would it be fair to assume that you have reduced the problem to updating of only one table? If not, then that's the first step you should take. Find the smallest possible example which fails before you start posting the example. (For more information read SSCCE <-- click that link.)
 
ashwin bala
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

I checked it again.Without restarting the tomcat, if i insert the record everything is fine and records are getting inserted.But if i restart the tomcat and insert the records, all the
previous rows in the table are getting deleted.I understand that this is because I did not commit my previous transaction correctly.But I have used the transaction.commit() statement
in my code.Can you please help me in this ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashwin bala wrote:
Without restarting the tomcat, if i insert the record everything is fine and records are getting inserted.But if i restart the tomcat and insert the records, all the
previous rows in the table are getting deleted.I understand that this is because I did not commit my previous transaction correctly.


I don't think this is related to transactions. What does your persistence.xml or hibernate.conf.xml (or some such configuration file) look like? There's a setting in there which tells the persistence provider whether to recreate the database or just use the existing database. I guess you have set it to recreate from a clean state every time you restart/redeploy the application.
 
ashwin bala
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr.Jai kiran .It worked. I changed the property of "hbm2ddl.auto" from create to update in hibernate.cfg.xml. thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic