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

前端 未结 13 2322
轮回少年
轮回少年 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<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();
    }
    
    0 讨论(0)
  • 2020-11-21 05:23

    To whom searches for default value...

    It is written in the source code at version 2.0.5 of spring-boot and 1.1.0 at JpaProperties:

        /**
         * DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto"
         * property. Defaults to "create-drop" when using an embedded database and no
         * schema manager was detected. Otherwise, defaults to "none".
         */
        private String ddlAuto;
    
    0 讨论(0)
  • 2020-11-21 05:25

    Since this is a very common question, this answer is based on an article I wrote on my blog.

    First, the possible values for the hbm2ddl configuration property are the following ones:

    • none - No action is performed. The schema will not be generated.
    • create-only - The database schema will be generated.
    • drop - The database schema will be dropped.
    • create - The database schema will be dropped and created afterward.
    • create-drop - The database schema will be dropped and created afterward. Upon closing the SessionFactory, the database schema will be dropped.
    • validate - The database schema will be validated using the entity mappings.
    • update - The database schema will be updated by comparing the existing database schema with the entity mappings.

    I dedicated a blog post for the most common Hibernate DDL generation strategies:

    1. The hibernate.hbm2ddl.auto="update" is convenient but less flexible if you plan on adding functions or executing some custom scripts.
    2. The most flexible approach is to use Flyway.

    However, even if you use Flyway, you can still generate the initial migration script using hbm2ddl. In this article, you can see how you can combine the JPA Entity Model with jOOQ Table Model.

    0 讨论(0)
  • 2020-11-21 05:27
    • validate: validates the schema, no change happens to the database.
    • update: updates the schema with current execute query.
    • create: creates new schema every time, and destroys previous data.
    • create-drop: drops the schema when the application is stopped or SessionFactory is closed explicitly.
    0 讨论(0)
  • 2020-11-21 05:28

    There's also the undocumented value of "none" to disable it entirely.

    0 讨论(0)
  • 2020-11-21 05:30

    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.

    update: it alters the existing table in the database when you perform operation. You can add or remove columns with this option of hbm2ddl. But if you are going to add a new column that is 'NOT NULL' then it will ignore adding that particular column to the DB. Because the Table must be empty if you want to add a 'NOT NULL' column to the existing table.

    0 讨论(0)
提交回复
热议问题