在jpa中ddl-auto一共有四种:

分别为:

ddl-auto:create ----每次运行该程序,没有表格会新建表格,表内有数据会清空;
ddl-auto:create-drop ----每次程序结束的时候会清空表
ddl-auto:update ---- 每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
ddl-auto: validate ---- 运行程序会校验数据与数据库的字段类型是否相同,不同会报错。

上图为properties配置文件的配置项:

使用1)spring.jpa.hibernate.ddl-auto=create

    运行的sql为:  

Hibernate: drop table if exists auth_user
Hibernate: create table auth_user (id bigint not null, account varchar(32), name varchar(32), pwd varchar(64), primary key (id)) engine=InnoDB
Hibernate删掉已经存在表, 并重建表,恐怖!!!

使用2)spring.jpa.hibernate.ddl-auto=create-drop

    运行的sql为:

Hibernate: drop table if exists auth_user
Hibernate: drop table if exists cardo
Hibernate: create table auth_user (id bigint not null, account varchar(32), name varchar(32), pwd varchar(64), primary key (id)) engine=InnoDB
Hibernate: create table cardo (id bigint not null, brand_id integer, brand_name varchar(16), primary key (id)) engine=InnoDB
...
Hibernate: drop table if exists auth_user
Hibernate: drop table if exists cardo

使用3)spring.jpa.hibernate.ddl-auto=update

    运行的sql为:

Hibernate: create table auth_user (id bigint not null, account varchar(32), name varchar(32), pwd varchar(64), primary key (id)) engine=InnoDB
Hibernate: create table cardo (id bigint not null, brand_id integer, brand_name varchar(16), primary key (id)) engine=InnoDB

给entity类添加一个字段others, 表也会自动同步添加一个字段others.

@Entity
@Table(name = "AUTH_USER")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserDO {
@Id
private Long id;
@Column(length = 32)
private String name;
@Column(length = 32)
private String account;
@Column(length = 64)
private String pwd;
@Column(length = 255)
private String others; }

添加个字段others

    执行的sql为:

Hibernate: alter table auth_user add column others varchar(255)

给表添加了字段others.

表添加一个字段, 对entity类有啥影响?
没有任何影响.
Hibernate: insert into auth_user (account, name, others, pwd, id) values (?, ?, ?, ?, ?)

不会校验entity中字段类型和表中对应的字段的类型是否匹配。

使用4)spring.jpa.hibernate.ddl-auto=validate

当表中字段others是varchar类型, 实体类entity的others是Integer类型,
类型不匹配报错:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [others] in table [auth_user]; found [varchar (Types#VARCHAR)], but expecting [integer (Types#INTEGER)]

使用5)spring.jpa.hibernate.ddl-auto=none

禁止ddl

由于ddl-auto不能同时指定多个属性, 只能在create, create-drop, update, validate, none中选择一个属性

总结:
一般选择validate/update/none
绝对不能选 create, create-drop

update能帮助建表。

如果希望实体类发生改动而数据库表做出相应的更改且不破坏数据库现有的数据,要将spring.jpa.hibernate.ddl-auto属性值设置为update

这里还有一点,就算把ddl-auto设置成update值,也不能识别对表结构的所有更改,往往只能识别出增加的字段,比如修改字段名,修改字段类型或者删除一个字段都是不能够识别的。

springdata jpa之ddl-auto配置的属性的更多相关文章

  1. 【串线篇】spring boot整合SpringData JPA

    一.SpringData简介 其中SpringData JPA底层基于hibernate 二.整合SpringData JPA JPA: Java Persistence API的简称,中文名Java ...

  2. hibernate.hbm2ddl.auto配置详解

    hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下:<properties><property name="hibernate. ...

  3. Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】

    原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...

  4. SpringData JPA的学习笔记之环境搭建

    一.环境搭建 1.加入jar包   spring jar+jpa jar +springData jar >>SpringData jar包     2.配置applicationCont ...

  5. Spring、SpringMVC、SpringData + JPA 整合详解

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...

  6. SpringBoot整合SpringData JPA入门到入坟

    首先创建一个SpringBoot项目,目录结构如下: 在pom.xml中添加jpa依赖,其它所需依赖自行添加 <dependency> <groupId>org.springf ...

  7. Spring Boot + Jpa(Hibernate) 架构基本配置

    本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2 ...

  8. SpringData JPA示例

    SpringData JPA只是SpringData中的一个子模块 JPA是一套标准接口,而Hibernate是JPA的实现 SpringData JPA 底层默认实现是使用Hibernate 1.  ...

  9. SpringBoot + Jpa(Hibernate) 架构基本配置

    1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...

  10. SpringData JPA快速入门和基本的CRUD操作以及Specifications条件查询

    SpringData JPA概述: SpringData JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作 ...

随机推荐

  1. CPU飙升的问题

    本文转载自CPU飙升的问题 问题发现 事情是这样的,最近小码仔负责的项目预定今天凌晨2点上进行版本更新.前几天测试小姐姐对网站进行压力测试,观察服务的CPU.内存.load.RT.QPS等各种指标. ...

  2. Java NIO wakeup实现原理

    本文转载自Java NIO wakeup实现原理 导语 最近在阅读netty源码时,很好奇Java NIO中Selector的wakeup()方法是如何唤醒selector的,于是决定深扒一下wake ...

  3. spring扩展点整理

    本文转载自spring扩展点整理 背景 Spring的强大和灵活性不用再强调了.而灵活性就是通过一系列的扩展点来实现的,这些扩展点给应用程序提供了参与Spring容器创建的过程,好多定制化的东西都需要 ...

  4. [源码解析] 消息队列 Kombu 之 基本架构

    [源码解析] 消息队列 Kombu 之 基本架构 目录 [源码解析] 消息队列 Kombu 之 基本架构 0x00 摘要 0x01 AMQP 1.1 基本概念 1.2 工作过程 0x02 Poll系列 ...

  5. vue3使用路由

    下载 npm install vue-router@4 配置路由 暴露出一个createRouter方法,用来创建路由对象 通过defineAsyncComponent方法来实现路由的懒加载(文章1. ...

  6. go mod包管理 加代理下载

    原始go.mod文件 module xxx go 1.14 报错 i/o timeout go mod init workorder go mod init: go.mod already exist ...

  7. docker的安装和基本的docker命令、镜像和容器的操作

    1.yum 包更新到最新 yum update 2.安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的 yum insta ...

  8. spring-cloud-alibaba之Nacos

    在微服务构架中,集群服务间的需要调用时就需要知道各个服务的IP和提供服务的端口等信息,如果每个部署一个服务就配置一次,那么必然时非常麻烦的,因此我们需要一个能够统一管理的东西来解决这个问题,由此诞生了 ...

  9. [源码分析] 消息队列 Kombu 之 Consumer

    [源码分析] 消息队列 Kombu 之 Consumer 目录 [源码分析] 消息队列 Kombu 之 Consumer 0x00 摘要 0x01 综述功能 0x02 示例代码 0x03 定义 3.1 ...

  10. 迷宫问题(DFS)

    声明:图片及内容基于https://www.bilibili.com/video/BV1oE41177wk?t=3245 问题及分析 8*8的迷宫,最外周是墙,0表示可以走,1表示不可以走 设置迷宫 ...