在使用hibernate映射的表中添加列,而不会丢失现有数据

时间:2023-01-27 11:02:30

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 ?

我有一个名为Person的表,我已经在hibernate中映射了我已经有一些我不想放松的数据了。我需要添加一个名为address的新列,任何想法如何在hibernate中做到这一点?

Thanks in Advance..

提前致谢..

3 个解决方案

#1


0  

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

可能你不会*使用Hibernate来创建/更新数据库模式。我假设你的配置中有这样的东西:

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

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

只需将值更改为“validate”,对映射执行更改并分别执行ALTER TABLE语句。

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.

其他选项是使用“更新”让Hibernate弄清楚如何更新表结构。我建议将它保存在您的手中,只需手动执行DDL SQL。

#2


6  

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.

如果当前表是由Hibernate生成的,则只需在地址列的java实体类中添加address属性即可。然后将hibernate.hbm2ddl.auto属性设置为update,并且hibernate将在下次构建SessionFactory时自动创建此列。当hibernate.hbm2ddl.auto更新时,Hibernate不会更改数据库中的任何数据存储。

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.

或者,您可以手动发出SQL以更改表结构,然后在地址列的java实体类中添加地址属性。

#3


1  

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

您还应该阅读其他SO问题/答案:Hibernate:hbm2ddl.auto =生产中的更新?在设置hibernate.hbm2ddl.auto进行更新之前。

#1


0  

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

可能你不会*使用Hibernate来创建/更新数据库模式。我假设你的配置中有这样的东西:

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

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

只需将值更改为“validate”,对映射执行更改并分别执行ALTER TABLE语句。

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.

其他选项是使用“更新”让Hibernate弄清楚如何更新表结构。我建议将它保存在您的手中,只需手动执行DDL SQL。

#2


6  

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.

如果当前表是由Hibernate生成的,则只需在地址列的java实体类中添加address属性即可。然后将hibernate.hbm2ddl.auto属性设置为update,并且hibernate将在下次构建SessionFactory时自动创建此列。当hibernate.hbm2ddl.auto更新时,Hibernate不会更改数据库中的任何数据存储。

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.

或者,您可以手动发出SQL以更改表结构,然后在地址列的java实体类中添加地址属性。

#3


1  

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

您还应该阅读其他SO问题/答案:Hibernate:hbm2ddl.auto =生产中的更新?在设置hibernate.hbm2ddl.auto进行更新之前。