2

I have a simple MySQL database with some valid data that I mapped to a java class using hibernate framework.

Now I change some database table fields in MySQL and want to update my model (e.g. java classes).

I don't want to remap my database because I've done some changes in model classes that need them.

Is there any way to update the model without losing any data?

I used Netbeans 8.0 and Hibernate 4.x .

Is there a wizard to do this like when updating model in entity framework (Visual Studio) for Netbeans?

Thanks.

Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
Morteza Zakeri
  • 166
  • 1
  • 7

3 Answers3

1

As a quick and blunt tool for checking your entities and fields respectively correspond to your tables and columns, you could try hbm2ddl.auto = validate. But this doesn't appear to check relations / constraints - see here. AFAIK there's no easy way of doing that: The only way I can think of would be using Hibernate Tools reverse engineering - there's a few steps to go through with this and some guides available online if you Google it. Ideally you'd want to do this on a "before" and "after" version of the database to generate two sets of entities in two different folders and then diff the changes between these folders (using your favourite textual diff tool, e.g. WinMerge). Then finally manually merge these into your existing code base.

To avoid having do to this kind of thing repeatedly in future, you might want to consider setting up some processes to keep a good record of the database schema changes being made and/or to ensure the database and entities are always kept in sync.

Community
  • 1
  • 1
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
0

Why you don't write some SQL scripts to synchronize your java classes with database or vice versa?

I usually write a lot of scripts to alter, insert or delete data, etc.

Is about this you are talking?

Carlos Spohr
  • 507
  • 11
  • 24
0

Recommended way for RDBMS is using migration tools and write migration scripts, for example, see: Flyway or Liquibase.

Additionally, you can verify DB on startup using hbm2ddl.auto = validate as suggested by @Steve.

aux
  • 1,589
  • 12
  • 20