SpringBoot+JPA初始化数据库表(Springboot + JPA initializes database tables)

本文参考

  • 用属性控制Hibernate生成DDL
  • SpringBoot启动时初始化数据库及spring.jpa.generate-dll与spring.jpa.hibernate.ddl-auto之间的困惑
  • SpringBoot启动时初始化数据库及spring.jpa.generate-dll与spring.jpa.hibernate.ddl-auto之间的困惑
  • 60.1 Initialize a database using JPA

SpringBoot项目中初始化数据库的两种方法

  • 让SpringBoot根据脚本初始化表结构
  • 让JPA根据实体类进行初始化表结构

SpringBoot根据脚本初始化

结构初始化脚本文件由spring.datasource.schema属性指定,数据初始化脚本文件spring.datasource.data属性指定。这两个脚本是否执行的开关由spring.datasource.initialization-mode决定:always-一定执行,embedded-只对内存数据库执行,never-不执行。

JPA根据类结构初始化

SpringBoot+JPA自动建表:spring.jpa.generate-ddl

JPA可以根据我们定义的@Entity实体类,来自动生成表结构。在SpringBoot+JPA项目中, 我们可以通过定义属性spring.jpa.generate-ddl的值来控制这种行为。spring.jpa.generate-ddl是JPA层次的抽象,他和具体的JPA实现无关;默认值是false,如果你显式设置为true,最后框架会选择update方式。一般不建议设置这个属性值

SpringBoot+Hibernate自动建表:spring.jpa.hibernate.ddl-auto

Hibernate作为JPA规范的实现框架,也可以根据我们定义的@Entity实体类,来自动生成表结构。在SpringBoot+Hibernate项目中,我们可以通过定义属性spring.jpa.hibernate.ddl-auto的值来控制是否执行,可选的值为

  • create表示每次应用启动连接数据库的时候,都会将之前的表全部drop掉,重新根据实体类生成一遍。
  • create-drop 在create的基础上,在应用关闭的时候还会drop一次。每次启动应用,删除之前的表,关闭应用时,删除这次应用启动建立的表。
  • update可能是比较常用的,每次启动的时候会看看实体类有什么变化,然后看需不需要更改表结构。但是它只能add column/constraint, 不会删除原来的column/constraint,也不会修改column的数据类型。如果实体类的某个@Column属性删除了,重新启动下应用,对应的字段并不会被删除。
  • validate不会对表进行更改,但是会看看他和实体类是否对应,如果实体类的field类型和表column的数据类型不一致,会报错。
  • none什么都不做
  • 生产环境,强烈建立用none或者就不设置这个属性

spring.jpa.generate-ddl和spring.jpa.hibernate.ddl-auto

  • spring.jpa.generate-dll是jpa层面对数据表生成策略的控制,和具体的JPA实现无关。而spring.jpa.hibernate.ddl-auto是jpa实现hibernate层面上对数据表生成策略的控制。
  • spring.jpa.generate-dll比spring.jpa.hibernate.ddl-auto有更强的控制力度,即使spring.jpa.hibernate.ddl-auto为none,只要spring.jpa.generate-dll为true,也会根据@Entity注解的实体类生成对应数据表。
  • 前者会影响到后者,为了避免混淆和不好理解,这两者最好不要混用。在实践中,直接hibernate层面的属性spring.jpa.hibernate.ddl-auto就行。

配置 

spring.jpa.hibernate.ddl-auto=update#spring.jpa.show-sql=true#spring.jpa.generate-ddl=true
————————

< strong > refer to < / strong >

  • 用属性控制Hibernate生成DDL
  • SpringBoot启动时初始化数据库及spring.jpa.generate-dll与spring.jpa.hibernate.ddl-auto之间的困惑
  • SpringBoot启动时初始化数据库及spring.jpa.generate-dll与spring.jpa.hibernate.ddl-auto之间的困惑
  • 60.1 Initialize a database using JPA

< strong > two methods of initializing database in springboot project < / strong >

  • Let springboot initialize the table structure according to the script
  • Let JPA initialize the table structure according to the entity class

< strong > springboot initializes according to the script < / strong >

The structure initialization script file is created by spring datasource. The schema attribute specifies the data initialization script file spring datasource. Specified by the data property. The switch whether these two scripts execute is determined by spring datasource. Initialization mode determines: always – must be executed, embedded – only executed on the in memory database, never – not executed.

< strong > JPA initializes according to the class structure < / strong >

SpringBoot+JPA自动建表:spring.jpa.generate-ddl

JPA can automatically generate the table structure according to the @ entity entity class defined by us. In the springboot + JPA project, we can define the property spring jpa. The value of generate DDL to control this behavior. spring. jpa. Generate DDL is a JPA level abstraction, which has nothing to do with the specific JPA implementation; The default value is false. If you explicitly set it to true, the framework will choose the update method. Setting this property value is generally not recommended

SpringBoot+Hibernate自动建表:spring.jpa.hibernate.ddl-auto

As the implementation framework of JPA specification, hibernate can also automatically generate table structure according to the @ entity entity class defined by us. In the springboot + Hibernate project, we can define the property spring jpa. hibernate. DDL auto value to control whether to execute. The optional values are

  • Create means that every time the application starts to connect to the database, all the previous tables will be dropped and generated again according to the entity class.
  • Create drop on the basis of create, it will drop once when the application is closed. Every time you start the application, delete the previous table. When you close the application, delete the table created this time.
  • Update may be commonly used. Each time you start it, you will see what changes the entity class has, and then see whether you need to change the table structure. However, it can only add column / constraint, not delete the original column / constraint, nor modify the data type of column. If an @ column attribute of an entity class is deleted, restart the next application, and the corresponding field will not be deleted.
  • Validate will not change the table, but will check whether it corresponds to the entity class. If the field type of the entity class is inconsistent with the data type of the table column, an error will be reported.
  • None does nothing
  • The production environment is strongly established with none or this attribute is not set

spring.jpa.generate-ddl和spring.jpa.hibernate.ddl-auto

  • spring.jpa.generate-dll是jpa层面对数据表生成策略的控制,和具体的JPA实现无关。而spring.jpa.hibernate.ddl-auto是jpa实现hibernate层面上对数据表生成策略的控制。
  • spring.jpa.generate-dll比spring.jpa.hibernate.ddl-auto有更强的控制力度,即使spring.jpa.hibernate.ddl-auto为none,只要spring.jpa.generate-dll为true,也会根据@Entity注解的实体类生成对应数据表。
  • The former will affect the latter. In order to avoid confusion and poor understanding, it is best not to mix the two. In practice, the hibernate level attribute spring jpa. hibernate. DDL auto will do.

Disposition

spring.jpa.hibernate.ddl-auto=update#spring.jpa.show-sql=true#spring.jpa.generate-ddl=true