• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate is overwriting my database records

 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to Hibernate, and am just trying it out via a simple, throw-away Swing-based application. I am using MySQL and the org.hibernate.dialect.MySQLDialect. My class in question is Genre, containing the fields:

It contains appropriate getters and setters, along with a no-parameter constructor. The setter for the "id" field is private, per the suggestion of the tutorial that I have been following. I have a Genre.hbm.xml file with this class, among other things, it contains:


The problem I'm experiencing is that when I launch the application for the first time, I can enter new Genres and save them to MySQL with no problems. From the MySQL command line, I can easily see the new rows in the GENRES table. Their ID values begin at 1, and auto-increment (2, 3, etc). But then if I quit the application and then relaunch it, and then I create a new Genre, all of the previous rows in GENRES are deleted, and then the new Genre is inserted. That new row in GENRES will again contain an ID of 1.

I took a look at each step of the operation. The deletion of the previous rows occurs when I try to create the new Genre after relaunching the application. In other words, simply quitting the application does not clear out the previous rows, nor does subsequently relaunching the application. It's only after I do both of those things AND create/insert a new Genre.

Obviously, the desired result is that when I relaunch the application and add a new Genre, that new Genre is added to the previous Genres in the GENRES table; the previous rows should not be deleted.

Any thoughts on why this might be happening? Thanks in advance.
 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case anyone cares, the problem seems to be that I needed a config param like the following:

<property name="hbm2ddl.auto">update</property>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic