继续关注我的C语言学习博客

林临的地盘

java学习博客
posts - 44, comments - 28, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

用hibernate建表

Posted on 2006-07-28 16:55 lubaolin 阅读(3312) 评论(0)  编辑  收藏
用hibernate建表

把xxx.hbm.xml文件放在class所在位置。偶测试时,执行正常,但未见数据库中的表,不知道哪里出错。

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

xxx {
    Configuration conf= new Configuration().addClass(xxx.class);
    //第一次运行时用来在数据库中创建表
    //并且把sql语句输出到txt文件用的
    //以后的运行要去掉这一段,否则每次都新建表
    Properties prop = new Properties();
    //没有设置hibernate.dialect时,执行无法通过,还要找另外一种代替方法,否则这里跟数据库类型绑定,不是一种好的实现方式
    prop.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.MySQLDialect";
    SchemaExport dbExport=new SchemaExport(conf, prop);
    dbExport.setOutputFile("sql_out_lib\\sql.txt";
    dbExport.create(true, true);
}


dbExport.create(true,true);

 把这个语句的第二个参数true改成false,就可以生成sql语句了,变成下面的语句:

 dbExport.create(true, false);

 不过无法直接执行,还需要继续摸索。
 另外,发现这个时候不需要数据库的连接,所以hibernate配置文件的连接池没有设置也没有关系。

引自:http://www.blogcn.com/user17/yu_qj/blog/6763871.html


只有注册用户登录后才能发表评论。


网站导航:
 
继续关注我的C语言学习博客