关于java:如何关闭hbm2ddl?

How to turn off hbm2ddl?

我找不到关于如何关闭hbm2ddl的参考。


只是忽略hibernate.hbm2ddl.auto默认为Hibernate没有做任何事情。从参考文档:

1.1.4. Hibernate configuration

The hbm2ddl.auto option turns on
automatic generation of database
schemas directly into the database.
This can also be turned off by removing the configuration option,
or redirected to a file with the help
of the SchemaExport Ant task.

hbm2ddl.auto设置为none(未记录)可能会生成警告,例如:org.hibernate.cfg.SettingsFactory - Unrecognized value for"hibernate.hbm2ddl.auto": none


您可以通过以下方式将其关闭:

1
hibernate.hbm2ddl.auto=none

它没有证件但是无价!


要明确这一点,应该查看org.hibernate.cfg.SettingsFactory的来源(根据使用的版本,您可能会看到其他内容):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
String autoSchemaExport = properties.getProperty( AvailableSettings.HBM2DDL_AUTO );
if ("validate".equals(autoSchemaExport) ) {
    settings.setAutoValidateSchema( true );
}
else if ("update".equals(autoSchemaExport) ) {
    settings.setAutoUpdateSchema( true );
}
else if ("create".equals(autoSchemaExport) ) {
    settings.setAutoCreateSchema( true );
}
else if ("create-drop".equals( autoSchemaExport ) ) {
    settings.setAutoCreateSchema( true );
    settings.setAutoDropSchema( true );
}
else if ( !StringHelper.isEmpty( autoSchemaExport ) ) {
    LOG.warn("Unrecognized value for "hibernate.hbm2ddl.auto":" + autoSchemaExport );
}

org.hibernate.cfg.Settings类中,这些变量初始化为:

1
2
3
4
private boolean autoCreateSchema;
private boolean autoDropSchema;
private boolean autoUpdateSchema;
private boolean autoValidateSchema;

所以这些默认为false。

省略hibernate.hbm2ddl.auto设置应该按照建议的hibernate.hbm2ddl.auto = none关闭HBM2DDL_AUTO功能,但在后一种情况下,您会在日志中收到警告。


在hibernate.properties中

1
hibernate.hbm2ddl.auto=validate

当然,配置它的地方取决于你配置你的hibernate的方式 - 如果是以编程方式,在那里设置属性。如果是来自hibernate.cfg.xml:

1
<property name="hibernate.hbm2ddl.auto">validate</property>


如果输入不受支持的值,它将告诉您支持哪些值:
o.h.b.i.SessionFactoryBuilderImpl : Unrecognized hbm2ddl_auto value : bla. Supported values include 'create', 'create-drop', 'update', 'none' and 'validate'. Ignoring

none是默认值,正式支持并记录:
https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl


此属性不是必需的。只需从xml文件中完全删除hibernate.hbm2ddl.auto条目即可。