关键字: hibenate hbm2ddl
hibenate.hbm2ddl.auto属性详解 :
hibernate 配置属性中,hibernate.hbm2ddl.auto可以帮助你实现正向工程,即由java代码生成数据库脚本,进而生成具体的表结构. 在hibernate.cfg.xml中:
<property name="hibernate.hbm2ddl.auto">update</property>
它包含4个属性:
* create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变
* create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除
* update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
* validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值
在hibernate中,如果在hibernate.cfg.xml文件中,将hibernate.hbm2ddl.auto设置为update(或者cretae-drop)也可以,如下 <property name="hibernate.hbm2ddl.auto">update</property>
则在运行应用程序时(第一次),会自动建立起表的结构(前提是先建立好数据库)。要注意的是, 当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会建立.
spring配置文件中配置则为
update
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${dialect}
hibernate.hbm2ddl.auto=update
hibernate.show_sql=${hibernate.show_sql}
hibernate.cache.provider_class=${hibernate.cache.provider_class}
hibernate.cache.use_second_level_cache=${hibernate.cache.use_second_level_cache}
hibernate.cache.use_query_cache=${hibernate.cache.use_query_cache}
</value>
</property>
<property name="mappingDirectoryLocations" ref="mappingDirectoryLocations" />
<property name="mappingJarLocations" ref="mappingJarLocations" />
<property name="mappingLocations" ref="mappingLocations" />
</bean>
注:省略了jdbc.properties 文件
hibernate.cfg.xml配置文件中配置则为
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库的连接信息(JDBC connection properties (begin)) -->
<!-- mysql连接
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
-->
<!--sql server 2000 连接
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433;databasename=demo</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
-->
<!-- sql server 2005~2012 org.hibernate.dialect.SQLServerDialect-->
<property name="hibernate.dialect">com.vrv.dsms.sqlserverdialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://192.168.120.95:1433;databasename=demo</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<!--Oracle连接
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:demo</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
-->
<!--配置C3P0数据源-->
<property name="c3p0.min_size">10</property>
<property name="c3p0.max_size">50</property>
<property name="c3p0.timeout">2000</property>
<property name="c3p0.max_statements">50</property>
<property name="c3p0.acquire_increment">2</property>
<property name="c3p0.idle_test_period">60</property>
<!-- hebernate属性信息 -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.jdbc.batch_size">100</property>
<!-- 实体映射文件 -->
<mapping resource="com/demo/domain/Employee.hbm.xml" />
....
</session-factory>
</hibernate-configuration>