Detailed description of hibernate automatic table creation configuration, detailed description of hibernate Creation

Source: Internet
Author: User

Detailed description of hibernate automatic table creation configuration, detailed description of hibernate Creation

Detailed description of the configuration of automatic table creation by hibernate

Configure Automatic table creation:

<Prop key = "hibernate. hbm2ddl. auto"> update </prop> // used when creating a project for the first time. After the project is stable, note that there are four values:
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.

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.

Then, in the configuration, read the object class ing:

There are three methods:

1. (commonly used) method of scanning packets by annotation:

<property name="packagesToScan"><list><value>com.systop.common.core.dao.testmodel</value></list>

2. Annotations:

<property name="annotatedClasses">  <list><value>com.systop.common.core.dao.testmodel.TestDept</value></list> </property> 

3. mappingResources is used to specify a small number of hibernate configuration files.

Xml Code

<property name="mappingResources">    <list>   <value>WEB-INF/conf/hibernate/cat.hbm.</value>         <value>WEB-INF/conf/hibernate/dog.hbm.xml</value>         ......    </list> </property> 

Instance:

<Bean id = "propertyConfigurer" class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "> <list> <value> classpath *:/dataSource/jdbc. properties </value> </list> </property> </bean> <! -- Configure the data source --> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" $ {jdbcCcbs. driverClassName} "/> <property name =" url "value =" $ {jdbcCcbs. url} "/> <property name =" username "value =" $ {jdbcCcbs. username} "/> <property name =" password "value =" $ {jdbcCcbs. password} "/> </bean> <! -- Configure SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate4.LocalSessionFactoryBean"> <! -- <Bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.annotation. annotationSessionFactoryBean "> --> <property name =" dataSource "ref =" dataSource "/> <property name =" hibernateProperties "> <props> <prop key =" hibernate. dialect "> org. hibernate. dialect. mySQLDialect </prop> <! -- Automatically create table configuration --> <! -- <Prop key = "hibernate. hbm2ddl. auto "> update </prop> --> <prop key =" hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <! -- Result scroll set, related to paging --> <prop key = "jdbc. use_scrollable_resultset"> false </prop> </props> </property> <! -- <Property name = "annotatedClasses"> --> <! -- <List> --> <! -- <Value> com. jason. entity. UserEntity </value> --> <! -- </List> --> <! -- </Property> --> <property name = "packagesToScan"> <list> <value> com. jason. entity </value> </list> </property> </bean>

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.