3

we are using Hibernate to get Data from a DB. The Problem is that we have sometimes to change our Tables. Infact of that we got several times the Problem that the Database Mapping from Hibernate is not valid with the database.

Can someone give me a hint how to solve that problem?

Simple Example:

Today User Table looks like:

  • id
  • Name
  • Forename
  • Adress
  • sex
  • username

In the Futur it can look like:

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

It's important to hold it easy as possible.

We're using MsSQL and XML Mapping.

Im thankfull for every contructiv Answer.

Artur Rem
  • 209
  • 1
  • 4
  • 18

2 Answers2

2

In hibernate.cfg.xml file add property :

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

Available values :

validate | update | create | create-drop

By using this property you need to change the POJO and .hbm.xml files it will automatically update to database.

There is difference between create and update is that, update will update your database if database tables are already created and will create if database tables are not created.

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

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • Thank you for your answer. The hbm2ddl propety is already known, maybe i formulated my queston wrong. I'd like to keep my database as it is and need to update only my mapping to support a independed Project that can be used on any db scheme. It would be perfect if hibernate checks database and then updates my mapping. – Artur Rem Oct 21 '14 at 14:28
1

If your database and your OO code are being managed independently, then you probably cannot avoid this issue. However, if you're trying to evolve your data model centrally then you have a choice to make - do it from the OO side first, and get the database up to date, or modify the database and update the mappings in the OO side.

If you focus just on the DB then the mappings will just have to catch up. If you want to do this easier, then evolve the OO model and make sure the mappings you use make sense - use the DDL generation capability of hibernate to either automatically update the database, or write the DDL to a fresh DB so you can use DB comparison tools to propagate the updates.

Big changes will be breaking changes - it's pretty much unavoidable. In general, if your DB is purely there to support an OO system, then I would recommend designing it from the OO mapping first.

Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23
  • i would like to update my OO Mapping to support the case that i have to export my hibernate project to a customer with a older DB Structure, to keep my DB and to refresh my mapping automatically. Is that possible? – Artur Rem Oct 21 '14 at 14:36
  • If you're lucky, the hbm2ddl feature of hibernate MIGHT upgrade your customer's database, but it's a risk. http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do for more info. You would have to test this hugely. A better option is for you to use that feature to export the natural schema of your latest code to a fresh DB and then use a DB comparison tool (See Redgate software) to generate the script that needs to be run on the database to upgrade it to the structure your code now needs. – Ashley Frieze Oct 21 '14 at 15:05