以前使用Hibernate都是用最愚蠢的方式:先写实体类,然后SQL语句或者在PLSQL Developer工具中建立数据库相关的表,再写配置文件和映射文件.....
这种方式费时费力不说,还时常出现问题,比如类,包或者属性名称对应不上等等。其实这也不是Java提倡的面向对象开发思想。
现在强制转变到面向对象模型设计上:先写实体类,然后写配置文件和映射文件,再使用Hibernate提供的
hbm2ddl工具
SchemaExport类根据实体类,配置文件和映射文件生成相应的数据库结构。也许这面这张图片更能简单明了的表达这种编程思想:
Hibernate的hbm2dll提供
SchemaExport工具作用就是:给定一个连接字符串和映射文件,不需输入其他东西就可以按照持久化类和映射文件自动生成数据库架构。其实,
SchemaExport给提供了三个方法都可以完成对数据库结构的操作:
public void
create(boolean script, boolean export),
public void
drop(boolean script, boolean export),
public void
execute(boolean script, boolean export, boolean justDrop, boolean justCreate)
Create(script,export)方法根据持久类和映射文件先删除架构后创建删除数据库架构。有两个参数,第一个为True就是把DDL语句输出到控制台,第二个为True就是根据持久类和映射文件先执行删除再执行创建操作,经过调试可以发现这个方法其实质是执行Execute(script,export, false, true)方法。
Drop(script, export)方法根据持久类和映射文件执行删除数据库架构。有两个参数,第一个为True就是把DDL语句输出到控制台,第二个为True就是根据持久类和映射文件执行删除数据库架构操作,经过调试可以发现Drop(script, export)方法其实质是执行了Execute(script, export, true, true)方法。
Execute(script, export, justDrop, format)方法根据持久类和映射文件先删除架构后创建删除数据库架构。有四个参数,第一个为True就是把DDL语句输出到控制台;第二个为True就是根据持久类和映射文件在数据库中先执行删除再执行创建操作;第三个为false表示不是仅仅执行Drop语句还执行创建操作,这个参数的不同就扩展了上面两个方法;第四个参数为false表示不是格式化输出DDL语句到控制台,是在一行输出的。
1.实体类:Student.java
public
class Student {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
//一系列setter.getter方法
}
2.配置文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<
hibernate-configuration
>
<
session-factory
>
<
property
name
="hibernate.connection.url"
>jdbc:oracle:thin:@localhost:1521:test
</
property
>
<
property
name
="hibernate.connection.driver_class"
>oracle.jdbc.driver.OracleDriver
</
property
>
<
property
name
="hibernate.connection.username"
>SCOTT
</
property
>
<
property
name
="hibernate.connection.password"
>
yf123
</
property
>
<
property
name
="hibernate.dialect"
>org.hibernate.dialect.Oracle9Dialect
</
property
>
<
property
name
="hibernate.show_sql"
>true
</
property
>
<
mapping
resource
="com/yf/hibernate/Student.hbm.xml"
/>
</
session-factory
>
</
hibernate-configuration
>
3.映射文件:Student.hbm.xml
<?
xml
version
="1.0"
?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<
hibernate-mapping
>
<
class
name
="com.yf.hibernate.Student"
table
="hibernate_student"
>
<
id
name
="id"
length
="6"
>
<
generator
class
="uuid"
/>
</
id
>
<
property
name
="name"
length
="10"
/>
<
property
name
="password"
length
="8"
/>
<
property
name
="createTime"
/>
<
property
name
="expireTime"
/>
</
class
>
</
hibernate-mapping
>
4.利用SchemaExport工具让Hibernate自动生成数据库结构
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public
class ExportDB {
public
static
void main(String[] args) {
// 读取hibernate.cfg.xml文件
Configuration cfg =
new Configuration().configure();
SchemaExport export =
new SchemaExport(cfg);
export.create(
true,
true);
//第一个为True就是把DDL语句输出到控制台,第二个为True就是根据持久类和映射文件执行删除数据库架构操作
}
}
运行结果:
本文转自NightWolves 51CTO博客,原文链接:http://blog.51cto.com/yangfei520/269302
,如需转载请自行联系原作者