Does Hibernate create tables in the database automatically

后端 未结 8 1165
庸人自扰
庸人自扰 2020-12-01 07:23

Upon reading this (archived) tutorial, they have not mentioned anything over creating tables in the DB. Does the Hibernate handle it automatically by creating tables and fie

相关标签:
8条回答
  • 2020-12-01 07:58

    your hibernate.hbm2ddl.auto setting should be defining that the database is created (options are validate, create, update or create-drop)

    0 讨论(0)
  • 2020-12-01 07:59

    Yes it does in your case because of the below property in your config. This is ok during testing but in production you need to disable this.

    <prop key="hibernate.hbm2ddl.auto">create</prop>
    
    0 讨论(0)
  • 2020-12-01 08:01

    yes you can use

    <property name="hbm2ddl.auto" value="create"/>
    
    0 讨论(0)
  • 2020-12-01 08:02

    If property hibernate.ddl-auto = update, then it will not create the tables automatically. To create tables automatically, you need to set the property to hibernate.ddl-auto = create

    The list of option which is used in the spring boot 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 at the end of the session

    • none: is all other cases

    So for the first time you can set it to create and then next time on-wards you should set it to update.

    0 讨论(0)
  • 2020-12-01 08:04

    For me it wasn't working even with hibernate.hbm2ddl.auto set to update. It turned out that the generated creation SQL was invalid, because one of my column names (user) was an SQL keyword. This failed softly, and it wasn't obvious what was going on until I inspected the logs.

    0 讨论(0)
  • 2020-12-01 08:04

    Yes, Hibernate can be configured by way of the hibernate.hbm2ddl.auto property in the hibernate.cfg.xml file to automatically create tables in your DB in order to store your entities in them if the table doesn't already exist.

    This can be handy during development where a new, in-memory, DB can be used and created a new on every run of the application or during testing.

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