About a configuration parameter Hibernate. hbm2ddl. auto in hibernate

Source: Internet
Author: User


In hibernate. cfg. xml, the hibernate. hbm2ddl. auto Configuration node 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 is explained as follows:
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-drop


In fact, this hibernate. hbm2ddl. auto parameter is mainly used to: automatically create | update | verify the database table structure. If this is not the requirement, set value = "none" is recommended ".
Create:
Every 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 is an important cause of data loss in database tables.
Create-drop:
Every time hibernate is loaded, a table is generated based on the model class. However, when sessionFactory is disabled, the table is automatically deleted.
Update:
The most common attribute. When hibernate is loaded for the first time, the table structure will be automatically created based on the model class (the premise is that the database is established first). When hibernate is loaded, the table structure will be 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.
Validate:
Each time hibernate is loaded, the system verifies the creation of the database table structure and only compares it with the database table. A new table is not created, but a new value is inserted.
Let's talk about "nonsense ":
When hibernate. hbm2ddl. auto = create is used, 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 during the first operation, the schema can be used after subsequent application restart. If you modify the ing or database schema, you must re-open hbm2ddl.


Share:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.