A very high level check is look through your hibernate class meta data (after session factory is initialized) and do an HQL query to a select a row off each table in the database. If you have data in every table, this tests Hibernate mapping each table/column name to their java equivalent.
It will catch some simple mistakes like:
1. Columns that have mismatched names between your mappings and DB.
2. Datatypes that don't match up between your mappings and DB.
3. Columns in the DB that have been dropped.
4. Tables that have been renamed.
5. Tables that have been dropped.
Wrap it in a catch block, and you have an error message for each table telling you what doesn't match up right.
After you have successfully checked whether the database is in sync with your application, you can invoke the SchemaExport task programmatically in your code.
Originally, I investigated using JDBC meta-data to evaluate our database structure, but unfortunately, some of the meta-data (foreign keys) was hidden to our DB2 connection so we had to go with the aforementioned approach.
If you can see all the db meta-data, doing an extensive comparison would be the best approach -- though definately more code intensive.
There is a related topic metioned somewhere on the hibernate wiki or docs that does a "from Object" HQL query to load all persistant instances. I suspect it would break on the first error and be a bit of a performance hit on startup (if you have a decent sized database).
|