Hibernate automatically generate database tables

Source: Internet
Author: User

Add in Hibernate.cfg.xml:

Reference

<properties>
<property name= "hibernate.hbm2ddl.auto" value= "create"/>
</properties>


the values of value can be selected as follows:

Reference

  • validation of creating database table structure when validate loading hibernate
  • Create to recreate the database table structure each time hibernate is loaded.
  • Create-drop When loading hibernate is created, exit is delete table structure
  •  update hibernate automatically updates database structure

Hibernate Reference Documentation 3.3.1 explained as follows:
Automatically validate or export schema DDL to the database, the sessionfactory is created.
With Create-drop, the database schema would be dropped if the sessionfactory is closed explicitly.
eg. Validate | Update | Create | Create-drop


Actually thisThe role of the Hibernate.hbm2ddl.auto parameter is mainly used to: automatically create| update| Validates the database table structure. If it's not a requirement in this areaSet value= "None".
Create:
Per loadHibernate will delete the last generated table, and then according to yourThe model class then re-generated the new table, even if two times without any changes to do so, this is the result of database table data loss is an important reason.
Create-drop:
Per loadHibernate is based onThe model class generates the table, butSessionfactory one off, the table is automatically deleted.
Update:
The most commonly used property is that the first time you load hibernate, the structure of the table is automatically built according to the model class (provided that the database is established first ) and later loaded Hibernate automatically updates the table structure according to the model class, even if the table structure changes but the rows in the table still exist and the previous rows are not deleted. It is important to note that when deployed to a server, the table structure is not immediately established, it is to wait for the application to run for the first time.
Validate :
Each time hibernate is loaded, validation creates a database table structure that is only compared to tables in the database and does not create new tables, but inserts new values.

Say it again. "nonsense ":
when we put < Span id= "1_14" >hibernate.hbm2ddl.auto=create hibernate first Use the Span lang= "en-us" >hbm2ddl to generate the database schema.
when we put hibernate.cfg.xml file hbm2ddl property commented out, so we canceled the hbm2ddl at startup to generate the database schema. It is usually only necessary to open it when repeating unit tests repeatedly, but running HBM2DDL will erase everything you have saved (drop) ----the meaning of the Create configuration is: "when creating sessionfactory, from scema drop out the tables, and then re-create them .
/span>

Note that many hibernate novices will fail at this step, and we occasionally see questions about the Table not found error message. However, as long as you follow the steps described above, there is no problem because HBM2DDL will create the database schema the first time it is run and continue to use the schema after the subsequent application restarts . If you modify the mapping, or modify the database schema, you must reopen the hbm2ddl.

Hibernate automatically generate database tables

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.