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..
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.
Hibernate always updates all database columns mapped by my entity, even the ones that I didn't change.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With