ITEEDU

Hibernate Gossip: 从映像文件生成数据表

在您撰写好*.hbm.xml映射文件之后,您可以使用org.hibernate.tool.hbm2ddl.SchemaExport来自动建立数据库表格,假设您的User.hbm.xml如下:
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 name="onlyfun.caterpillar.User"
table="user">

<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>

<property name="name" column="name" type="java.lang.String"/>

<property name="age" column="age" type="java.lang.Integer"/>

</class>

</hibernate-mapping>
在hibernate.cfg.xml中设定JDBC等相关设定:
hibernate.cfg.xml
<?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">

<hibernate-configuration>

<session-factory>

<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/demo</property>
<!-- 数据库使用者 -->
<property name="connection.username">caterpillar</property>
<!-- 数据库密码 -->
<property name="connection.password">123456</property>
<!-- C3P0 连接池设定 -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>


<!-- 对象与数据库表格映像文件 -->
<mapping resource="onlyfun/caterpillar/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

可撰写一个程序如下:
HbmToTable.java
package onlyfun.caterpillar;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class HbmToTable {
public static void main(String[] args) {
Configuration config = new Configuration().configure();
System.out.println("Creating tables...");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
}
}
运行程序之后,将会有以下的结果:
Creating tables...
10:39:10,203 DEBUG SchemaExport:143 - drop table if exists user
create table user (
     id integer not null auto_increment,
     name varchar(255),
     age integer,
     primary key (id)
 )
 10:39:10,203 DEBUG SchemaExport:161 - create table user (
     id integer not null auto_increment,
     name varchar(255),
     age integer,
     primary key (id)
)

10:39:10,359  INFO SchemaExport:173 - schema export complete
生成的资料表如下:
+--------+-----------------+------+------+----------+---------------------+
| Field    | Type              | Null | Key | Default | Extra                    |
+--------+-----------------+------+------+----------+---------------------+
| id         | int(11)            |          | PRI | NULL    | auto_increment |
| name   | varchar(255) | YES  |        | NULL    |                              |
| age      | int(11)            | YES  |        | NULL    |                              |
+--------+-----------------+------+-----+-----------+---------------------+
3 rows in set (0.00 sec)