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

来源:互联网 发布:洪拳小子知乎 编辑:程序博客网 时间:2024/04/29 04:28

电脑重装系统忘记备份数据库了。 用项目里面的   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 {  } }}