Javaxy 2008-10-28 21:13
浏览 285
已采纳

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

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语句

  • 写回答

2条回答

  • liuqaing_0701 2008-10-29 14:00
    关注

    首先我鄙视你一下,哈哈…………

    你把
    create table t_order (id integer not null auto_increment, name varchar(255), desc varchar(255), tim datetime, customer_id integer, primary key (id))

    这个拿到mysql运行不就得了,结果报错!

    什么原因呢??
    desc varchar(255)中的desc是关键字,换个名字就可以了!

    这个不是hibernate的原因,完全是数据库的问题!!
    ok , 换个名字吧!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)