Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

java - How to create database schema in hibernate first time and further update it in case of schema modification?

I want to create database schema in hibernate first time. And further, if there is any modification in the schema, like addition of a new table or deletion of some column, I want to update the existing schema keeping the previous data intact.

As per options given at this question, it looks like either I can create schema destroying the previous data, or I can update the schema.

Is there any value which can do both?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually I just checked <property name="hibernate.hbm2ddl.auto" value="update" /> is even creating tables for the first time and then later if table/schema exist it does update.

Update property is applicable when starting or adding a new model. You want to retain the earlier saved entity instances. This is the default schema creation style.

It tries to update the schema, if required. The following updates are supported:

See some of my observations

  • Add a field - A new column is added to the table.
  • Rename a field - A new column is added to the table, while the original column remains but is not used any longer. Note: The data from the old column is not migrated to the new column.
  • Remove a field - The column remains but is not used.
  • Change a field type - The type of the column does not change, which may result in type-mismatch exceptions.
  • Create an entity - Creates a new table.
  • Rename an entity - Creates a new table, while the original table remains.
  • Move an entity to another folder - Creates a new table, while the original table remains.
  • Delete an entity - The table remains.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...