[英]Adding columns to a Hibernate table with data
I have a class in my application that is being serialized by Hibernate, and there are already several rows in the corresponding table in my database. 我的应用程序中有一个要由Hibernate序列化的类,并且数据库的对应表中已经有几行。 If I need to add fields to this class after the table is created, do I need to do anything special?
创建表后,如果需要在此类中添加字段,是否需要做一些特别的事情? Drop the table and let Hibernate start over?
放下桌子,让Hibernate重新开始吗? Or can I just add a properly annotated field and let Hibernate do the rest?
或者我是否可以添加一个带有适当注释的字段,然后让Hibernate完成其余的工作?
Hibernate can update tables in place, if you configure: 如果您配置,Hibernate可以在适当的位置更新表:
<prop key="hibernate.hbm2ddl.auto">update</prop>
See the documentation: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html . 请参阅文档: http : //docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html 。 See Table 3.7 for details.
有关详细信息,请参见表3.7。
@Aaron is correct. @亚伦是正确的。 The only issue might be if one of the new columns has a not null constraint.
唯一的问题可能是新列之一具有非null约束。 If so you can use a column definition attribute to give the field a default value.
如果是这样,您可以使用列定义属性为该字段提供默认值。
See here https://forum.hibernate.org/viewtopic.php?f=1&t=982553&view=previous and here http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ for more details. 详情请参阅https://forum.hibernate.org/viewtopic.php?f=1&t=982553&view=previous和http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.