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

前端 未结 13 2466
轮回少年
轮回少年 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:31

    If you don't want to use Strings in your app and are looking for predefined constants have a look at org.hibernate.cfg.AvailableSettings class included in the Hibernate JAR, where you'll find a constant for all possible settings. In your case for example:

    /**
     * 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";
    
    0 讨论(0)
  • 2020-11-21 05:36

    I would use liquibase for updating your db. hibernate's schema update feature is really only o.k. for a developer while they are developing new features. In a production situation, the db upgrade needs to be handled more carefully.

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

    The configuration property is called hibernate.hbm2ddl.auto

    In our development environment we set hibernate.hbm2ddl.auto=create-drop to drop and create a clean database each time we deploy, so that our database is in a known state.

    In theory, you can set hibernate.hbm2ddl.auto=update to update your database with changes to your model, but I would not trust that on a production database. An earlier version of the documentation said that this was experimental, at least; I do not know the current status.

    Therefore, for our production database, do not set hibernate.hbm2ddl.auto - the default is to make no database changes. Instead, we manually create an SQL DDL update script that applies changes from one version to the next.

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

    Although it is quite an old post but as i did some research on the topic so thought of sharing it.

    hibernate.hbm2ddl.auto

    As per the documentation it can have four valid values:

    create | update | validate | create-drop

    Following is the explanation of the behaviour shown by these value:

    • create :- create the schema, the data previously present (if there) in the schema is lost
    • update:- update the schema with the given values.
    • validate:- validate the schema. It makes no change in the DB.
    • create-drop:- create the schema with destroying the data previously present(if there). It also drop the database schema when the SessionFactory is closed.

    Following are the important points worth noting:

    • In case of update, if schema is not present in the DB then the schema is created.
    • In case of validate, if schema does not exists in DB, it is not created. Instead, it will throw an error:- Table not found:<table name>
    • In case of create-drop, schema is not dropped on closing the session. It drops only on closing the SessionFactory.
    • In case if i give any value to this property(say abc, instead of above four values discussed above) or it is just left blank. It shows following behaviour:

      -If schema is not present in the DB:- It creates the schema

      -If schema is present in the DB:- update the schema.

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

    From the community documentation:

    hibernate.hbm2ddl.auto 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.

    e.g. validate | update | create | create-drop

    So the list of possible options are,

    • validate: validate the schema, makes no changes to the database.
    • update: update the schema.
    • create: creates the schema, destroying previous data.
    • create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.
    • none: does nothing with the schema, makes no changes to the database

    These options seem intended to be developers tools and not to facilitate any production level databases, you may want to have a look at the following question; Hibernate: hbm2ddl.auto=update in production?

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

    hibernate.hbm2ddl.auto automatically validates and exports DDL to the schema when the sessionFactory is created.

    By default, it does not perform any creation or modification automatically on DB. If the user sets one of the below values then it is doing DDL schema changes automatically.

    • create - doing creating a schema

      <entry key="hibernate.hbm2ddl.auto" value="create">
      
    • update - updating existing schema

      <entry key="hibernate.hbm2ddl.auto" value="update">
      
    • validate - validate existing schema

      <entry key="hibernate.hbm2ddl.auto" value="validate">
      
    • create-drop - create and drop the schema automatically when a session is starts and ends

      <entry key="hibernate.hbm2ddl.auto" value="create-drop">
      
    0 讨论(0)
提交回复
热议问题