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?
2 Answers
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.
Comments
@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.
null? There are few enough rows that I can fill in that column by hand, as long as Hibernate doesn't complain or crash.