Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate Hibernate mapping against database

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.

like image 525
Juha Syrjälä Avatar asked Jan 22 '10 10:01

Juha Syrjälä


People also ask

What is Validator in Hibernate?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.

How mapping is done in hibernate?

Define Hibernate Mapping FileThe <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.


1 Answers

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

like image 107
Bozho Avatar answered Oct 30 '22 04:10

Bozho