springdata jpa之ddl-auto配置的属性
在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配置的属性的更多相关文章
- 【串线篇】spring boot整合SpringData JPA
一.SpringData简介 其中SpringData JPA底层基于hibernate 二.整合SpringData JPA JPA: Java Persistence API的简称,中文名Java ...
- hibernate.hbm2ddl.auto配置详解
hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下:<properties><property name="hibernate. ...
- Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】
原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...
- SpringData JPA的学习笔记之环境搭建
一.环境搭建 1.加入jar包 spring jar+jpa jar +springData jar >>SpringData jar包 2.配置applicationCont ...
- Spring、SpringMVC、SpringData + JPA 整合详解
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...
- SpringBoot整合SpringData JPA入门到入坟
首先创建一个SpringBoot项目,目录结构如下: 在pom.xml中添加jpa依赖,其它所需依赖自行添加 <dependency> <groupId>org.springf ...
- Spring Boot + Jpa(Hibernate) 架构基本配置
本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2 ...
- SpringData JPA示例
SpringData JPA只是SpringData中的一个子模块 JPA是一套标准接口,而Hibernate是JPA的实现 SpringData JPA 底层默认实现是使用Hibernate 1. ...
- SpringBoot + Jpa(Hibernate) 架构基本配置
1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...
- SpringData JPA快速入门和基本的CRUD操作以及Specifications条件查询
SpringData JPA概述: SpringData JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作 ...
随机推荐
- ubuntu无法连接有线网
问题描述: ubuntu下仅能连接无线网,不能连接有线网,在有线网的下面是没有选项可供连接. 解决方法: 编辑 /etc/network/interfaces 这个文件 将里面仅仅写两句话 auto ...
- c/c++ 之静态库
静态库 编译成目标文件(未链接) g++ -c a.cc b.cc c.cc d.cc #生成 a.o b.o c.o d.o 将目标文件打包为静态库 ar rs libxxx.a a.o b.o c ...
- AWS Switching to an IAM role (AWS CLI)
一,引言 今天额外分享一篇 AWS 的技术内容,需要在 EC2 切换到跨账号 IAM 角色(AWS CLI).假设我们使用两个 AWS 账户,A账号,B账号.我们希望允许 A 账号用于 "i ...
- 痞子衡嵌入式:盘点国内RISC-V内核MCU厂商(2020年发布产品)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是国内RISC-V内核MCU厂商(2020). 虽然RISC-V风潮已经吹了好几年,但2019年才是其真正进入主流市场的元年,最近国内大量 ...
- go 报错 import cycle not allowed
运行时报错,import cycle not allowed : 查了goole大概知道了原因,还是导包类的问题,我检察了一下我的代码库,发现我昨天划分几个工具文件,里面的两个文件相互引用,就导致报i ...
- 【白话科普】CDN & 游戏加速器,两者是一个原理吗?
说起加速,大家可能就会联想到"游戏加速"之类的场景,而说到现在流行的云服务加速,则离不开 CDN 这个词.那么 CDN 和游戏加速器是同一种东西么?从效果上看两者都是为了" ...
- go中waitGroup源码解读
waitGroup源码刨铣 前言 WaitGroup实现 noCopy state1 Add Wait 总结 参考 waitGroup源码刨铣 前言 学习下waitGroup的实现 本文是在go ve ...
- 快速查找未打补丁的exp
在windows DOS窗口下输入以下内容,输出为未打的补丁信息列表 systeminfo>vul.txt&(for %i in (KB977165 KB2160329 KB250366 ...
- centos /bin /sbin /usr/bin /usr/sbin 目录的说明
在linux下我们经常用到的四个应用程序的目录是/bin./sbin./usr/bin./usr/sbin .而四者存放的文件一般如下: bin目录: bin为binary的简写主要放置一些系 ...
- Hibernate在oracle中ID增长的方式(续)
引用链接:http://blog.csdn.net/w183705952/article/details/7367272 第二种:设置ID的增长策略是native,但是需要创建一个名字为hiberna ...