我真的想知道更多关于更新,导出和值,可以给hibernate.hbm2ddl.auto 我需要知道什么时候使用更新,什么时候不?另一种选择是什么?

以下是在DB中可能发生的变化:

新表 旧表中的新列 列删除 事件解释列的数据类型发生变化 列的类型更改了其属性 表了 事件解释列值发生变化

在每种情况下,最佳解决方案是什么?


当前回答

如果你不想在你的应用程序中使用字符串,并正在寻找预定义的常量,请查看Hibernate JAR中包含的org.hibernate.cfg.AvailableSettings类,在那里你可以找到一个用于所有可能设置的常量。以你为例:

/**
 * Auto export/update schema using hbm2ddl tool. Valid values are <tt>update</tt>,
 * <tt>create</tt>, <tt>create-drop</tt> and <tt>validate</tt>.
 */
String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";

其他回答

validate: It validates the schema and makes no changes to the DB. Assume you have added a new column in the mapping file and perform the insert operation, it will throw an Exception "missing the XYZ column" because the existing schema is different than the object you are going to insert. If you alter the table by adding that new column manually then perform the Insert operation then it will definitely insert all columns along with the new column to the Table. Means it doesn't make any changes/alters the existing schema/table.

更新:在执行操作时,会改变数据库中已有的表。 您可以使用hbm2ddl的这个选项添加或删除列。 但是如果你要添加一个'NOT NULL'的新列,那么它将忽略向DB添加特定的列。因为如果你想在现有的表中添加“NOT NULL”列,表必须是空的。

来自社区文档:

hibernate.hbm2ddl。auto在创建SessionFactory时自动验证或将模式DDL导出到数据库。使用create-drop,当SessionFactory显式关闭时,数据库模式将被删除。 例如:validate | update | create | create-drop

所以可能的选项是,

Validate:验证模式,不对数据库进行任何更改。 Create-only:创建数据库。 Drop:生成数据库删除。 Update:更新模式。 Create:创建模式,销毁之前的数据。 create-drop:当SessionFactory显式关闭时删除模式,通常是在应用程序停止时。 None:不对模式进行任何操作,不对数据库进行任何更改

这些选项似乎是开发人员的工具,而不是为了方便任何生产级别的数据库,你可能想看看下面的问题;Hibernate: hbm2ddl。自动=更新生产?

从5.0开始,您现在可以在专用Enum: org.hibernate.boot. schemaauto工装中找到这些值(从5.2开始使用值NONE进行了增强)。

或者更好的是,从5.1开始,您还可以使用org.hibernate.tool.schema.Action Enum,它结合了JPA 2和“遗留的”Hibernate DDL操作。

但是,您还不能用它以编程方式配置数据源。与org.hibernate.cfg结合使用会更好。AvailableSettings#HBM2DDL_AUTO但是当前代码期望一个String值(摘自SessionFactoryBuilderImpl):

this.schemaAutoTooling = SchemaAutoTooling.interpret( (String) configurationSettings.get( AvailableSettings.HBM2DDL_AUTO ) );

和org.hibernate.boot. schemaautotooling和org.hibernate.tool.schema.Action的内部enum值都没有公开。

以下是一个示例编程数据源配置(在我的Spring Boot应用程序中使用),它使用了.name().toLowerCase(),但它只适用于没有破折号的值(例如,不是create-drop):

@Bean(name = ENTITY_MANAGER_NAME)
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier(DATA_SOURCE_NAME) DataSource internalDataSource) {

    Map<String, Object> properties = new HashMap<>();
    properties.put(AvailableSettings.HBM2DDL_AUTO, SchemaAutoTooling.CREATE.name().toLowerCase());
    properties.put(AvailableSettings.DIALECT, H2Dialect.class.getName());

    return builder
            .dataSource(internalDataSource)
            .packages(JpaModelsScanEntry.class, Jsr310JpaConverters.class)
            .persistenceUnit(PERSISTENCE_UNIT_NAME)
            .properties(properties)
            .build();
}

如果你不想在你的应用程序中使用字符串,并正在寻找预定义的常量,请查看Hibernate JAR中包含的org.hibernate.cfg.AvailableSettings类,在那里你可以找到一个用于所有可能设置的常量。以你为例:

/**
 * Auto export/update schema using hbm2ddl tool. Valid values are <tt>update</tt>,
 * <tt>create</tt>, <tt>create-drop</tt> and <tt>validate</tt>.
 */
String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";

综上所述…… 注意,这个属性叫做dll。auto和应该只控制dll操作(创建/删除模式/表),我惊讶地发现它也与dml有关:只有更新将允许插入数据,这是dml操作。

在试图将数据填充到内存数据库时被此捕获;只有更新有效。