Use hibernate's ddl-auto under jpa
Today I made a report function and found that the shopProductId in a table is null, but in the program, I judged that the shopProductId is used, and the productId in the table is not null. On the way to find the reason, I learned that someone changed the productId to shopProductId, but the data has not been updated in the past.
In this case, how can we see that some fields have been changed?
Since the framework we use is springBoot+jap-hibernate, then in the hibernate under jpa, in the application configuration file, there is
ddl-auto configuration
-
ddl-auto:create
Every time you run the program, if there is no table, a new table will be created, and if there is data in the table, it will be cleared. -
ddl-auto:create-drop
The table will be cleared every time the program ends -
ddl-auto:update
Every time the program is run, a new table will be created without a table, and the data in the table will not be cleared, but will only be updated. -
ddl-auto:validate
Running the program will verify whether the field types of the data and the database are the same, and an error will be reported if it is different.
When we change ddl-auto to validate, when we start the project, we will prompt which field has been changed. In this way, when we publish and go online, we will find it in time and avoid errors. When running normally, it is usually set to the update property.
-auto configuration
-auto can be explicitly set -auto,
The standard Hibernate attribute values are none, validate, update, create, create-drop.
Spring Boot selects a default value based on whether the database is an inline type.
See below for specific relationships
Inline Type | Database name | default value |
---|---|---|
Inline | hsqldb, h2, derby | create-drop |
Non-embedded | Other databases | none |
The meanings of the four attributes of -auto are shown in the following table:
Attribute value | effect |
---|---|
create | Every time hibernate is loaded, the last generated table will be deleted, and then a new table will be generated again according to your model class. Even if there is no change in the two times, it must be executed in this way. This is an important reason for the loss of database table data. |
create-drop | Each time hibernate is loaded, the table is generated according to the model class, but as soon as the sessionFactory is closed, the table is automatically deleted. |
update | The most commonly used attribute is that when hibernate is loaded for the first time, the table structure will be automatically established according to the model class (provided that the database is established first). When hibernate is loaded for the future, the table structure will be automatically updated according to the model class. Even if the table structure is changed, the rows in the table still exist and the previous rows will not be deleted. It should be noted that when deployed to the server, the table structure will not be established immediately, and it will not be completed after the application is first run. |
validate | Every time hibernate is loaded, verification creates the database table structure, which only compares with the tables in the database, does not create a new table, but a new value is inserted. |
In addition, files in the classpath root directory at startup will be executed (provided that the ddl-auto attribute is set to create or create-drop). This is useful when demos or testing, but may not be expected in production environments.
This is a feature of Hibernate and has nothing to do with Spring.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.