hibernate.hbm2ddl.auto 的设置

来源:互联网 发布:淘宝店属于什么行业 编辑:程序博客网 时间:2024/05/11 13:29
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration><session-factory><property name="connection.username">root</property><property name="connection.url">jdbc:mysql://localhost:3306/myhibernate</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hbm2ddl.auto">create</property><property name="c3p0.min_size">5</property><property name="c3p0.max_size">20</property><property name="c3p0.timeout">300</property><property name="c3p0.max_statements">50</property><property name="c3p0.idle_test_period">3000</property><property name="show_sql">true</property><property name="format_sql">true</property><property name="myeclipse.connection.profile">mysql</property><property name="connection.password"></property><mapping resource="hello/Message.hbm.xml" /></session-factory></hibernate-configuration>


alidate               加载hibernate时,验证创建数据库表结构
create                  每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop        加载hibernate时创建,退出是删除表结构
update                 加载hibernate自动更新数据库结构

在本机开发调试初始化数据的时候可以选择create、update等。


但是网站发布正式版本的时候,对数据库现有的数据或表结构进行自动的更新是很危险的。此时此刻应该由DBA同志通过手工的方式进行后台的数据库操作。

hibernate.hbm2ddl.auto的值建议是“none”或“validate”。“validate”应该是最好的选择:这样 spring在加载之初,如果model层和数据库表结构不同,就会报错,这样有助于技术运维预先发现问题。




原创粉丝点击