t;date">
<column name="modifier" />
</property>
<property name="creater" type="date">
<column name="creater" />
</property>
<property name="lastlogin" type="date">
<column name="lastlogin" />
</property>
</class>
</hibernate-mapping>
说明:具体的poweracl.hbm.xml要根据数据库表而设置,这里只是列举一个user表。
Hibernate:利用配置文件生成数据库(2)
时间:2009-10-10
(3) 初始化数据库类
package com.hibtest;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
/**
* <p/> vedadou
* Date: 2004-02-25
* Time: 9:40:15
*/
public class InitDB {
static Session session;
public static void main(String[] args) {
Configuration config = null;
Transaction tx = null;
try {
config = new Configuration().configure(new File("hibernate.cfg.xml"));
System.out.println("Creating tables...");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
注意:在初始化之前,应该先手工创建一个空数据库,然后再执行InitDB程序 |