您的位置:首页 > 数据库

hibernate.cfg.xml 和每张表对应的hbm文件 生成数据库表

2013-01-21 21:38 288 查看
电脑重装系统忘记备份数据库了。 用项目里面的 hibernate.cfg.xml 和每张表对应的hbm文件 生成数据库表的代码:

import java.io.File;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateSchemaExport {
static Session session;
static Configuration config = null;
static Transaction tx = null;
public static void main(String[] args) {
try {
config = new Configuration().configure(new File(
"src/hibernate.cfg.xml"));
System.out.println("Creating tables...");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐