9

I have a table called Person which I have already mapped in hibernate I has already some data which I do not want to loose. I need to add new column called address, Any idea how to do that in hibernate ?

Thanks in Advance..

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
finepax007
  • 651
  • 3
  • 8
  • 16

3 Answers3

7

If you current tables are generated by Hibernate , you can simply add the address property in the java entity class for the address column . Then set the hibernate.hbm2ddl.auto property to update and hibernate will automatically create this column when the SessionFactory is built next time . Hibernate will not change any data store in your database when hibernate.hbm2ddl.auto is update.

Or , you can manually issue the SQL to alter the table structure and then add the address property in the java entity class for the address column.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • I have set the property as update. But I don't understand, why still it is trying to create a table. It's firing create table rather then alter table. Can you please help me out here.? – Vaibhav Jain Sep 05 '18 at 17:50
2

Likely you are not forced to use Hibernate to create/update database schema. I assume you have something like this in your configuration:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

Just change value to "validate", perform changes to the mappings and execute ALTER TABLE statements separately.

Other option is to use "update" to let Hibernate figure out how to update your table structure. I suggest to keep it in your hands and just execute DDL SQL manually.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
1

You should also read this other SO question/answer: Hibernate: hbm2ddl.auto=update in production? before you set hibernate.hbm2ddl.auto to update.

Community
  • 1
  • 1
Randy Stegbauer
  • 1,104
  • 2
  • 11
  • 25