问题描述
如何检查Hibernate映射配置是否与数据库匹配?我想知道如果我使用错误版本的hibernate映射,然后开始执行更新和查询,然后会失败。
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.
我有很多类映射与Hibernate注释。我也有相应的数据库连接。现在我想检查Hibernate映射是否匹配数据库。
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:
- 所有映射字段都存在于数据库中
- 所有映射字段都有正确的类型
我更喜欢我不必对映射表执行查询,最好只是基于数据库元数据。 / p>
I'd prefer that I do not have to execute queries to mapped tables, preferably the check is based solely on database meta data.
推荐答案
从:
hibernate.hbm2ddl.auto
hibernate.hbm2ddl.auto
例如。验证|更新|创建| create-drop
e.g. validate | update | create | create-drop
因此,您可以将其设置为 validate
验证您的休眠映射中的所有内容是否都存在于数据库中。如果将它设置为 update
,则每次添加映射类或属性时,底层数据库模式都会更新以反映该更改。
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
这篇关于如何验证Hibernate映射对数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!