Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add column in a table mapped using hibernate, without losing existing data

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..

like image 790
finepax007 Avatar asked Mar 28 '12 18:03

finepax007


People also ask

How do I map one table to another table in Hibernate?

You need to annotate each attribute which you want to map to the secondary table with a @Column annotation and set the name of the secondary table as the value of the table attribute.

Does Hibernate update all fields?

Hibernate always updates all database columns mapped by my entity, even the ones that I didn't change.

What is @table in Hibernate?

The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database. The @Table annotation provides four attributes, allowing you to override the name of the table, its catalogue, and its schema, and enforce unique constraints on columns in the table.


1 Answers

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.

like image 82
Ken Chan Avatar answered Oct 16 '22 07:10

Ken Chan