Hibernate中利用配置文件(hbm)自动生成数据库表

        接上一篇文章,上篇文章介绍了如何利用XDoclet从类自动生成hbm配置文件,这篇写一下如何自动通过hbm文件自动建立数据库表 。以类为基础,生成配置文件和数据库表,更加符合OO。
       上一篇文章自动生成了Position.hbm.xml和Users.hbm.xml两个配置文件,将其加入hibernate.cfg.xml中,然后建立HibernateSchemaExport类,代码如下:
package  test;

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(
truetrue);

            System.out.println(
"Table created.");

            tx.commit();

        }
 catch (HibernateException e) {
            e.printStackTrace();
            
try {
                tx.rollback();
            }
 catch (HibernateException e1) {
                e1.printStackTrace();
            }

        }
 finally {

        }

    }


}


运行,出现如下输出:
Creating tables...
alter   table  test_user_position  drop   foreign   key  FKF1F5A2301D5E879B
alter   table  test_user_position  drop   foreign   key  FKF1F5A2307D008B16
drop   table   if   exists  test_position
drop   table   if   exists  test_user_position
drop   table   if   exists  test_uses
create   table  test_position (
    id 
integer   not   null  auto_increment,
    name 
integer ,
    
primary   key  (id)
)
create   table  test_user_position (
    position_id 
integer   not   null ,
    
user_id   integer   not   null ,
    
primary   key  ( user_id , position_id)
)
create   table  test_uses (
    id 
integer   not   null  auto_increment,
    name 
varchar ( 25 ),
    
primary   key  (id)
)
alter   table  test_user_position 
    
add   index  FKF1F5A2301D5E879B ( user_id ), 
    
add   constraint  FKF1F5A2301D5E879B 
    
foreign   key  ( user_id
    
references  test_uses (id)
alter   table  test_user_position 
    
add   index  FKF1F5A2307D008B16 (position_id), 
    
add   constraint  FKF1F5A2307D008B16 
    
foreign   key  (position_id) 
    
references  test_position (id)
Table  created.

现在看看数据库,已经成功地创建了 test_position、 test_uses和 test_user_position 三张表。

利用这两篇文章中的方法,可以先进行Java类的设计,再自动生成配置文件和数据库表,这样做更见符合OO的设计思想,但是如果遇到表与表之间关系复杂,可能就不是很适合了。嘿嘿
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值