18

How to check that Hibernate mapping configuration matches database? I'd like know if I am using wrong version of hibernate mapping before I start executing update and queries, which then would fail.

I have bunch of classes that have been mapped with Hibernate annotations. I also have connection to corresponding database. Now I'd like to check if Hibernate mapping matches the database.

I'd like to check at least following things:

  • all mapped tables in Hibernate configuration have corresponding object in database (e.g table or view)
  • all mapped fields exist in database
  • all mapped fields have correct types

I'd prefer that I do not have to execute queries to mapped tables, preferably the check is based solely on database meta data.

1 Answer 1

25

From Hibernate configuration docs:

hibernate.hbm2ddl.auto

Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.

e.g. validate | update | create | create-drop

So, you can set it to validate and it will verify if everything in your hibernate mappings is present in the database. If you set it to update, then every time you add a mapped class or property, the underlying db schema will be updated to reflect that change.

You also have a command-line tool - SchemaUpdate

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

3 Comments

Something like that I am looking for. But is there something in API that I could execute programatically?
You can execute it programmatically. Write a small program that creates a SessionFactory with "validate" turned on.
yes, there is a command line tool - I added it to my answer. Or preferably do what duffymo suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.