简体   繁体   中英

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. 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? Or can I just add a properly annotated field and let Hibernate do the rest?

Hibernate can update tables in place, if you configure:

<prop key="hibernate.hbm2ddl.auto">update</prop>

See the documentation: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html . See Table 3.7 for details.

@Aaron is correct. The only issue might be if one of the new columns has a not null constraint. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM