1

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?

3
  • What should the value of the new fields be for the objects already saved? Commented Oct 4, 2011 at 15:51
  • Well that's one of my quandaries. Will Hibernate just fill in a sensible default, like null? There are few enough rows that I can fill in that column by hand, as long as Hibernate doesn't complain or crash. Commented Oct 4, 2011 at 15:57
  • You can put any legal value you want into the data cell. Hibernate doesn't care what is there. It is the database that will determine the limits as long as you haven't modified the table that was created. Commented Oct 4, 2011 at 16:28

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

@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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.