Jpa in ddl-auto in a total of four:
They are as follows:
ddl-auto: create ---- each time you run the program, will not form a new table, the data will be cleared in the table;
DDL-Auto: the Create drop----- the end of each program will clear the table
ddl- auto: update ---- each time you run the program, will not form a new table, the data in the table is not empty, it will only update
ddl-auto: check data field types and databases validate ---- whether the program will run same, different error.
The picture shows the configuration item properties configuration file:
使用1)spring.jpa.hibernate.ddl-auto=create
sql run as follows:
Hibernate: Table drop 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 the InnoDB hibernate already deleted table, and rebuild the table, horror! ! !
使用2)spring.jpa.hibernate.ddl-auto=create-drop
sql run as follows:
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 run as follows:
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
Add a field to the entity class others, the table will automatically add a field sync 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 add fields
sql would be:
Hibernate: alter table auth_user add column others varchar(255)
Add fields to the table the others.
Table to add a field for the entity class so what impact?
No effect.
Hibernate: INSERT INTO auth_user (the Account, name, Others, pwd, the above mentioned id) values (,,,,?????)
Entity does not check the type field and the type field in the corresponding table match.
使用4)spring.jpa.hibernate.ddl-auto=validate
When the table is in the field of type varchar others, the entity class others entity is of type Integer,
type mismatch error:
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
Prohibit ddl
Because ddl-auto multiple attributes at the same time can not be specified, only a select attribute create, create-drop, update, validate, none of
Summary:
General Select validate / update / none
absolutely can not choose create, create-drop
update to help build the table.
If changes are made to the desired entity class database table changes accordingly does not damage the existing data in the database, to spring.jpa.hibernate.ddl-auto property value to update
Another point here, even if the ddl-auto is set to update values, can not recognize all the changing table structure, often only identified additional fields, such as changing field names, modify or delete a field type fields are not able to recognition of.