如何验证Hibernate映射对数据库 [英] How to validate Hibernate mapping against database

查看:184
本文介绍了如何验证Hibernate映射对数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查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配置文档

hibernate.hbm2ddl.auto

hibernate.hbm2ddl.auto


创建SessionFactory时,自动验证或导出模式DDL到数据库。使用create-drop,当SessionFactory显式关闭时,数据库模式将被删除。

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.

例如。验证|更新|创建| 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.

您还有一个命令行工具 - SchemaUpdate

You also have a command-line tool - SchemaUpdate

这篇关于如何验证Hibernate映射对数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆