Detailed explanation of hibernate.hbm2ddl.auto configuration

http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html

hibernate.hbm2ddl.auto configuration node in hibernate.cfg.xml is as follows:
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>

Hibernate Reference Documentation 3.3.1 explains this:
Automatically validate or export schema DDL to the database when the SessionFactory is created.
With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
eg. validate | update | create | create-drop




In fact, the function of this hibernate.hbm2ddl.auto parameter is mainly used for: automatic creation | update |Validate the database table structure. If this is not the case, it is recommended to set value="none".
create:
Every time hibernate is loaded, the last generated table will be deleted, and then a new table will be regenerated according to your model class, even if there is no change twice, this is an important reason for the loss of database table data.
create-drop:
Each time hibernate is loaded, a table is generated based on the model class, but the table is automatically deleted as soon as the sessionFactory is closed.
update:
the most commonly used attribute. 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), and the table structure will be automatically updated according to the model class when hibernate is loaded later, even if the table structure has changed. Rows in the table still exist without deleting previous rows. It should be noted that when deployed to the server, the table structure will not be established immediately, it will not be established until the application is run for the first time.
validate :
Every time hibernate is loaded, the database table structure is verified to be created, and it will only be compared with the tables in the database, no new tables will be created, but new values ​​will be inserted.

Let's say "nonsense":
when we set hibernate.hbm2ddl.auto=create, hibernate first uses hbm2ddl to generate the database schema.
When we comment out the hbm2ddl attribute in the hibernate.cfg.xml file, we cancel the use of hbm2ddl to generate the database schema at startup. Usually you only need to turn it on when you keep repeating unit tests, but running hbm2ddl again will delete everything you saved (drop) ---- the meaning of the create configuration is: "When creating a SessionFactory, from scema Drop all the tables and recreate them".
Note that many Hibernate newbies will fail at this step, and we'll see questions about Table not found error messages from time to time. However, as long as you follow the steps described above, you won't have this problem, because hbm2ddl will create the database schema the first time it runs, and subsequent application restarts can continue to use this schema. If you modify the mapping, or modify the database schema, you must reopen hbm2ddl again.

**************************************************** *********

In the past two days, I have been sorting out Spring + JPA (Hibernate implementation), and copied a section of Hibernate connection parameter configuration from the Internet.

<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
As a result, the database is always found during testing Table data is lost. This parameter was not used much before, I checked other stuff, and finally located it. Quickly check the parameter configuration of Hibernate, explained as follows:

hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-

drop The function of this parameter is mainly used to: automatically create|update|validate the database table structure. If it is not for this requirement, it is recommended to set value="none".

Let me explain the meaning of several other parameters:

validate When loading hibernate, verify the creation of the database table structure
create Every time hibernate is loaded, the database table structure is recreated, which is the result of The reason for the loss of database table data.
create-drop is created when loading hibernate, and exit is to delete the table structure
update load hibernate to automatically update the database structure The

above four properties work on all mapping tables used in the same configuration file.



Summary:

1. Please use this parameter with caution. Don't use it casually.

2. If you find that the database table is missing, please check the configuration of hibernate.hbm2ddl.auto

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991155&siteId=291194637