Hibernate database changes

we are using Hibernate to get data from DB. The problem is that we sometimes have to change our tables. Infact from the fact that we got several times the problem that the database mapping from Hibernate is not valid with the database.

Can anyone give me a hint how to fix this problem?

Simple example:

Today the user table looks like this:

  • id
  • Name
  • Forename
  • Address
  • sex
  • Username

In Futur it might look like this:

  • id
  • Name
  • Forename
  • fk_Adress
  • sex
  • Username
  • fk_Usergroup
  • fk_rightgroup

It is important to keep it as simple as possible.

We are using MsSQL and XML mapping.

I am grateful for every contructiv answer.

+3


source to share


2 answers


To hibernate.cfg.xml

add file property:

<property name="hibernate.hbm2ddl.auto">update</property>

      

Available Values:

check | update | create | create fall



Using this property, you need to modify the POJO files and .hbm.xml

which it will automatically update in the database.

There is a difference between create

and update

in that updating will update your database if the database tables are already created, and create if the database tables are not created.

create will create tables in the database, even if tables already exist, it will drop all tables and create tables again, resulting in previously inserted data loss.

+2


source


If your database and your OO code are managed independently, you probably cannot avoid this problem. However, if you want to centrally transform your data model, then you have a choice: do it from the OO side first, update the database, or change the database and update the mappings on the OO side.

If you only focus on the DB, then the mappings will just have to catch up. If you want to make it easier, then change the OO model and make sure the mappings you use make sense - use the hibernate DDL generation capabilities to automatically update the database or write the DDL to a new database so you can use the DB comparison tools to propagate updates.



Big changes will overwhelm change — this is largely inevitable. In general, if your DB exists solely to support an OO system, I would recommend building it from an OO mapping first.

+1


source







All Articles