What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

前端 未结 13 2426
轮回少年
轮回少年 2020-11-21 04:47

I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto
I need to know when to use the update and w

13条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 05:19

    Since 5.0, you can now find those values in a dedicated Enum: org.hibernate.boot.SchemaAutoTooling (enhanced with value NONE since 5.2).

    Or even better, since 5.1, you can also use the org.hibernate.tool.schema.Action Enum which combines JPA 2 and "legacy" Hibernate DDL actions.

    But, you cannot yet configure a DataSource programmatically with this. It would be nicer to use this combined with org.hibernate.cfg.AvailableSettings#HBM2DDL_AUTO but the current code expect a String value (excerpt taken from SessionFactoryBuilderImpl):

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

    … and internal enum values of both org.hibernate.boot.SchemaAutoToolingand org.hibernate.tool.schema.Action aren't exposed publicly.

    Hereunder, a sample programmatic DataSource configuration (used in ones of my Spring Boot applications) which use a gambit thanks to .name().toLowerCase() but it only works with values without dash (not create-drop for instance):

    @Bean(name = ENTITY_MANAGER_NAME)
    public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier(DATA_SOURCE_NAME) DataSource internalDataSource) {
    
        Map 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();
    }
    

提交回复
热议问题