In hibernate. cfg. XML, the hibernate. hbm2ddl. Auto Configuration node is as follows:
< Property Name = " Hbm2ddl. Auto " > Create-drop </ Property >
- Update: Auto ExecuteSchemaupdateOn buildsessionfactory
- Create: Auto ExecuteSchemaexportOn buildsessionfactory
- Create-drop: Auto ExecuteSchemaexportOn buildsessionfactory recreating the schema
- Validate: Auto ExecuteSchemavalidatorOn buildsessionfactory
The hbm2ddl. Auto parameter is mainly used to automatically create, update, and verify the database table structure. If not, we recommend that you set value = "NONE"
UPDATE: The most common attribute. When Hibernate is loaded for the first time, the structure of the table is automatically created based on the model class (the premise is that the database is created first). When Hibernate is loaded, the table structure is automatically updated based on the model class, even if the table structure is changed, the row in the table still does not delete the previous row. Note that after the application is deployed to the server, the table structure will not be created immediately. It will not be created until the application runs for the first time.
Create: each time Hibernate is loaded, the last generated table will be deleted, and then the new table will be re-generated based on your model class, even if there are no changes twice, it will be executed like this, this results in the loss of database table data.
Create-drop: each time Hibernate is loaded, a table is generated based on the model class. However, when sessionfactory is disabled, the table is automatically deleted.
Validate: each time Hibernate is loaded, the system verifies the structure of the created database table and only compares it with the table in the database. A new table is not created, but a new value is inserted.
<PropertyName="Hbm2ddl. Keywords">Auto-quote</Property>
- None: Disable any operation regarding RDBMS keywords
- Keywords:(Activated by default) Imports all RDBMS keywords where the NH-dialect can provide the implementationIdatabaseschema(So far available for MSSQL, Oracle, Firebird, mssqlce, MySQL, SQLite, sybaseanywhere)
- Auto-quote: Imports all RDBMS keywords and auto-quote all table-Names/column-names on buildsessionfactory
Note:
When we use hbm2ddl. Auto = create, Hibernate uses hbm2ddl to generate database schema.
When we comment out the hbm2ddl attribute in the hibernate. cfg. xml file, we cancel using hbm2ddl to generate database schema at startup. Usually you need to open it only when you perform unit tests repeatedly, but running hbm2ddl again will delete everything you saved (drop)-the meaning of the create configuration is: "Drop the tables from scema and recreate them when creating sessionfactory ".
Note: many new hibernate beginners will fail in this step. From time to time, we will see questions about the table not found error message. However, as long as you follow the steps described above, this problem will not occur, because hbm2ddl will create a database schema at the first run, and subsequent applications Program You can continue to use this schema after the restart. If you modify the ing or database schema, you must re-open hbm2ddl.
Generally, Hibernate is not recommended. hbm2ddl. auto creation or modification method, you can set hibernate. hbm2ddl. set auto value to none or validate to verify that the table structure is correct. The data table structure is manually generated.