Add a column to the table displayed by hibernation without losing existing data

I have a table called Person that I have already mapped in sleep mode. I already have some data that I do not want to lose. I need to add a new column called address, Any idea how to do this in sleep mode?

Thanks at Advance ..

+6
source share
3 answers

You should probably not use Hibernate to create / update the database schema. I assume that you have something like this in your configuration:

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

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

Another option is to use an β€œupdate” to let Hibernate figure out how to update the structure of your table. I suggest that you keep it in your hands and simply execute the DDL SQL manually.

0
source

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

Or you can manually fix SQL to change the structure of the table, and then add the address property to the java entity class for the address column.

+6
source

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

+1
source

Source: https://habr.com/ru/post/911880/


All Articles