GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Hibernate/Mysql - previous record(s) not being persisted. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate/Mysql - previous record(s) not being persisted." Watch "Hibernate/Mysql - previous record(s) not being persisted." New topic
Author

Hibernate/Mysql - previous record(s) not being persisted.

Rohit Mehta
Ranch Hand

Joined: Mar 11, 2005
Posts: 79

I am using MySQL with Hibernate, a small application, inserting record into table Person, which was created as :


Person.hbm.xml


Hibernate.cfg.xml::


To my surprise, every time I run app (so i am inserting a record) - record is getting updated with same id:

mysql> select * from person;
+-----------+-------------+
| PERSON_ID | PERSON_NAME |
+-----------+-------------+
| 1 | BOMBAYite |
+-----------+-------------+
1 row in set (0.00 sec)


NOTE --- I even tried by inserting more records by command line - they are gettign inserted & total records shoes 4.
But the moment I run my Hibernate app - all records are gone - just the latest (being inserted) is shown.

where my old record is going . please help.
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Rohit ,

Can you share the peace of code where you are setting Person bean and calling hibernate session to save the object.

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Rohit,

I thing the problem is with AUTO_INCREMENT=3 defined in your sql create statement.

when you are using hibernate native class, it issues an select statement to retrieve next id from database.
query is of format (select max(PERSON_ID) from person )
the same is also missing in your log file.

I would suggest you to try the above example without using "AUTO_INCREMENT=3 " option.



Regards,
Hemant

Rohit Mehta
Ranch Hand

Joined: Mar 11, 2005
Posts: 79

Thanks for your interest, Hemant. below is my code..


I tried without 'AUTO_INCREMENT=3' but the issue persist.
Rohit Mehta
Ranch Hand

Joined: Mar 11, 2005
Posts: 79

Finally I got it .. it's due to below line

<property name="hbm2ddl.auto">create</property>

commenting this line or changing the property to 'update' is persisting earlier inserted record.
Seems a basic stuff - but it's ok given I am just few days old in Hibernate' territory.
 
GeeCON Prague 2014
 
subject: Hibernate/Mysql - previous record(s) not being persisted.