Hibernate automatically generates database tables

Source: Internet
Author: User

You can add this statement to hibernate. cfg. XML to automatically generate a data table.
<Property name = "hibernate. hbm2ddl. Auto"> Update </property>


Update: automatically updates the table structure based on the model object. When Hibernate is started, the database is automatically checked. If a table is missing, the table is automatically created. If a column is missing in the table, the column is automatically added.

There are other parameters:
Create: When Hibernate is started, the original table is automatically deleted and all new tables are created. Therefore, the previous data is lost after each start.

Create-drop: When Hibernate is started, tables are automatically created. When the program is closed, all corresponding tables are automatically deleted. So at the end of the program, the table and data will no longer exist.

PS: the database needs to be created in advance, because hibernate only creates tables and does not create databases.

 

========================================================== =

The table structure and data are constantly modified without reason during program execution. After a long time, I checked the hibernate database ing files and interface programs and found no errors, it was discovered at the end!
<Property name = "hibernate. hbm2ddl. Auto" value = "Update"/>
Explanation:

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-drop

In fact, this parameter is mainly used to: automatically create | update | verify the database table structure.
If you do not have this requirement, we recommend that you set value = "NONE ".

Meanings of other parameters:

Verify the structure of the created database table when loading hibernate in validate.
Create loads hibernate each time and re-creates the database table structure
Create-drop is created when Hibernate is loaded. Exit is to delete the table structure
Update load hibernate automatically updates the database structure

If the database table is lost or newly added, check the configuration of hibernate. hbm2ddl. Auto to set <Property
Name = "hibernate. hbm2ddl. Auto" value = "NONE"
/>

You can add this statement to hibernate. cfg. XML to automatically generate a data table.
<Property name = "hibernate. hbm2ddl. Auto"> Update </property>


Update: automatically updates the table structure based on the model object. When Hibernate is started, the database is automatically checked. If a table is missing, the table is automatically created. If a column is missing in the table, the column is automatically added.

There are other parameters:
Create: When Hibernate is started, the original table is automatically deleted and all new tables are created. Therefore, the previous data is lost after each start.

Create-drop: When Hibernate is started, tables are automatically created. When the program is closed, all corresponding tables are automatically deleted. So at the end of the program, the table and data will no longer exist.

PS: the database needs to be created in advance, because hibernate only creates tables and does not create databases.

 

========================================================== =

The table structure and data are constantly modified without reason during program execution. After a long time, I checked the hibernate database ing files and interface programs and found no errors, it was discovered at the end!
<Property name = "hibernate. hbm2ddl. Auto" value = "Update"/>
Explanation:

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-drop

In fact, this parameter is mainly used to: automatically create | update | verify the database table structure.
If you do not have this requirement, we recommend that you set value = "NONE ".

Meanings of other parameters:

Verify the structure of the created database table when loading hibernate in validate.
Create loads hibernate each time and re-creates the database table structure
Create-drop is created when Hibernate is loaded. Exit is to delete the table structure
Update load hibernate automatically updates the database structure

If the database table is lost or newly added, check the configuration of hibernate. hbm2ddl. Auto to set <Property
Name = "hibernate. hbm2ddl. Auto" value = "NONE"
/>

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.