package com.xy.HibernateUtil;
import org.hibernate.cfg.Configuration;
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 ExportDB {
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 {
session.close();
}
}
}
通过这个类生成数据库的表,为什么明明显示执行了两条CREATE语句,生成了两个表,但是在数据库中只生成了一个表啊?
Creating tables...
alter table t_order drop foreign key FKA0C0C3C3734A7296
drop table if exists t_customer
drop table if exists t_order
create table t_customer (id integer not null auto_increment, name varchar(255), age integer, primary key (id))
create table t_order (id integer not null auto_increment, name varchar(255), desc varchar(255), tim datetime, customer_id integer, primary key (id))
alter table t_order add index FKA0C0C3C3734A7296 (customer_id), add constraint FKA0C0C3C3734A7296 foreign key (customer_id) references t_customer (id)
Table created.
这个是生成的SQL语句