在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. C++算法模板集合

    我的常用刷题网站:http://218.5.5.242:9018/JudgeOnline/ https://www.luogu.com.cn/ 排序 选择排序(selection sort) 1 vo ...

  2. winform导入excel或者csv

    if (txt01.Text != "") { this.lbzhantie.Items.Clear(); this.dtzhuanhuo.Rows.Clear(); if (tx ...

  3. 前端问题录——在导入模块时使用'@'时提示"Modile is not installed"

    前情提要 为了尽可能解决引用其他模块时路径过长的问题,通常会在 vue.config.js 文件中为 src 目录配置一个别名 '@' configureWebpack: { resolve: { a ...

  4. 第35天学习打卡(输入框 TextField监听 简易计算器,组合+内部类回顾复习 画笔 鼠标监听 窗口监听 键盘监听)

    1.输入框 TextField监听  package com.kuang.lesson02; ​ import java.awt.*; import java.awt.event.ActionEven ...

  5. Ubuntu的点滴-Vim、VM Tools、Samba、换源

    <div id="article_content" class="article_content clearfix"> <link rel=& ...

  6. 《深入浅出WPF》-刘铁猛学习笔记——XAML

    XAML是什么? XAML是微软公司创造的一种开发语言,XAML的全称是 Extensible Application Markup Language,即可拓展应用程序标记语言. 它由XML拓展而来, ...

  7. 【秒懂音视频开发】05_Qt开发基础

    控件的基本使用 为了更好地学习Qt控件的使用,建议创建项目时先不要生成ui文件. 打开mainwindow.cpp,在MainWindow的构造函数中编写界面的初始化代码. 窗口设置 MainWind ...

  8. EurekaServer源码分析

    Eureka Server功能 接受服务注册 接受服务心跳 服务剔除 服务下线 集群同步 获取注册表中服务实例信息 需要注意的是,Eureka Server同时也是一个Eureka Client,在不 ...

  9. 【Arduino学习笔记07】模拟信号的输入与输出 analogRead() analogWrite() map() constrain()

    模拟信号:Arduino中的模拟信号就是0v~5v的连续的电压值 数字信号:Arduino中的数字信号就是高电平(5V)或者低电平(0V),是两个离散的值 模拟信号->数字信号:ADC(模数转换 ...

  10. Linux发行版及其目标用户

    1.Debian Debian 众所周知,是Deepin,Ubuntu和Mint等流行Linux发行版的母亲,这些发行版提供了可靠的性能,稳定性和无与伦比的用户体验.最新的稳定发行版是Debian 1 ...