`
从此醉
  • 浏览: 1044152 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

Hibernate自动创建表

 
阅读更多

Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步。

一、通过Hibernate的ShemaExport来创建

1)实体类

package com.xiaomo.vo;
public class User {
private int id;// 用户id
private String name;// 用户名称
private int age;// 用户年龄
@Override
public String toString() {
return "id:"+this.id+"\tname:"+this.name+"\tage"+this.age;
}
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2)、User.hbm.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 用class元素来定义一个持久化类 -->
<class name="com.xiaomo.vo.User" table="user">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>

3)、hibernate.cfg.xml文件:

<!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在 Hibernate3.2.5软件包中的src\org\hibernate目录中找到此文件-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--声明Hibernate配置文件的开始-->
<hibernate-configuration>
<!-- 表明以下的配置是针对session-factory配置的,sessionFactory是hibernate中的一个类,这个类主要负责
保存hibernate的配置信息,以及对session的操作 -->
<session-factory>
<!-- 配置数据库的驱动程序,hibernate在连接数据库时,需要用到数据库的驱动程序 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 设置数据库的连接url:jdbc:mysql://localhost:3306/xiaomo,其中localhost表示说mysql的服务器名称,此处为本机。
port代表mysql服务器的端口号,默认为3306.xiaomo是数据库名,这是你要连接的数据库名 -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/lili
</property>
<!-- 如果你的mysql服务器都是默认设置的,且装在本机上则也可以写成
jdbc:mysql://localhost/xiaomo
或者是
jdbc:mysql:///test
-->
<!-- 连接数据库的用户名 -->
<property name="hibernate.connection.username">
root
</property>
<!-- 连接数据库的密码 -->
<property name="hibernate.connection.password">
cd_hisome
</property>
<!-- Hibernate使用的数据库方言,就是要用hibernate连接哪种类型的数据库服务器 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- hibernate.hbn2ddl.auto指定由java代码生成数据库脚本,进而生成具体的表结构的具体方式 -->
<property name="hbn2ddl.auto">update</property>
<!-- 是否在后台显示Hibernate生成的查询数据库的SQL语句,开发时设置为true,便于查询错误,运行时
可以在Eclipse的控制台显示Hibernate执行的sql语句。项目部署后可以设置为false,提高运行效率 -->
<property name="show_sql">true</property>
<!-- 指定映射文件为“com/xiaomo/vo/User.hbm.xml” -->
<mapping resource="com/xiaomo/vo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

4)、测试类:

package com.xiaomo.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {
public static void main(String[] args) {
//读取配置文件hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//创建SchemeExport实例
SchemaExport sExport = new SchemaExport(cfg);
//创建数据库表
sExport.create(true, true);
}
}

结果:

drop table if exists user
create table user (id integer not null auto_increment, name varchar(255), age integer, primary key (id))

这个时候查看数据库,可以看到表已经自动创建完毕。

二、在hibernate.cfg.xml里加上如下代码(这个方式网络上记载来的)

Xml代码<propertyname="hbm2ddl.auto">update</property>

1、update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表;如果表里缺少列,则自动添加列。

还有其他的参数:
2、create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后的以前数据都会丢失。

3、create-drop:启动hibernate时,自动创建表,程序关闭时,自动把相应的表都删除。所以程序结束时,表和数据也不会再存在。

注意:数据库要预先建立好,因为hibernate只会建表,不会建库


表结构和数据总是在程序执行的时候无端的修改,折腾了好长时间,查了很长时间hibernate的数据库映射文件和接口程序,始终没有发现有什么错误,到最后才发现了它!
<property name="hibernate.hbm2ddl.auto" value="update" />
解释如下:

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

其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。
如果没有此方面的需求建议set value="none".

其它几个参数的意思:

validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构

如果发现数据库表丢失或新增,请检查hibernate.hbm2ddl.auto的配置 可设置 <property name="hibernate.hbm2ddl.auto" value="none" />

建议在开发环境下使用,在生产环境下去掉。


hibernate自动创建表的优缺点:

一、优点:

1、自动创建新表

2、自动创建新字段

3、自动修改字段类型

二、缺点:

1、不会自动删除表

2、不会自动删除字段

3、自动创建的新字段只能是在最后。

针对缺点的建议:定期把数据库清空(删除所有表),然后启动项目,让hibernate自动创建表结构和索引,当然一些初始化数据需要手工导入。


出处:http://blog.csdn.net/cl05300629/article/details/16884739 作者:伫望碧落

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics