Hibernate根据映射文件生成数据库表

Javaxy 2008-10-28 08:59:51
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语句,生成了两个表,但是在数据库中只生成了一个表啊?
...全文
305 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
z295862154 2011-07-08
  • 打赏
  • 举报
回复
我都不知道怎么生成的 正在为这个问题纠结呢!!!
z295862154 2011-07-08
  • 打赏
  • 举报
回复
我都不知道怎么生成的 正在为这个问题纠结呢!!!
Javaxy 2008-10-29
  • 打赏
  • 举报
回复
大家回答的都没有什么关系。现在是产生了两条create语句,但是 在数据库中永远都只有一个表 !
zou_wei_forever 2008-10-29
  • 打赏
  • 举报
回复
config = new Configuration().configure(new File(
"src/hibernate.cfg.xml"));
System.out.println("Creating tables...");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
直接这样就可以了,没必要再开个session吧
xuhua205 2008-10-29
  • 打赏
  • 举报
回复
一般情况下是根据数据库表来生产Java文件和配置文件,那样简单而且不会出错!建议将你步骤反过来!真的很好用
l_wenb 2008-10-28
  • 打赏
  • 举报
回复
ooo
Javaxy 2008-10-28
  • 打赏
  • 举报
回复
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语句

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧