<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java Study Center  
    日歷
    <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789
    統(tǒng)計(jì)
    • 隨筆 - 40
    • 文章 - 3
    • 評(píng)論 - 0
    • 引用 - 0

    導(dǎo)航

    常用鏈接

    留言簿(1)

    隨筆檔案(40)

    文章檔案(3)

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

     
    接上一篇文章,上篇文章介紹了如何利用XDoclet從類自動(dòng)生成hbm配置文件,這篇寫一下如何自動(dòng)通過hbm文件自動(dòng)建立數(shù)據(jù)庫表 。以類為基礎(chǔ),生成配置文件和數(shù)據(jù)庫表,更加符合OO。
           上一篇文章自動(dòng)生成了Position.hbm.xml和Users.hbm.xml兩個(gè)配置文件,將其加入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) {
            
    /**
             * 根據(jù)映射文件創(chuàng)建數(shù)據(jù)庫結(jié)構(gòu)
             
    */

            
    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 {

            }

        }


    }


    運(yùn)行,出現(xiàn)如下輸出:
    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.

    現(xiàn)在看看數(shù)據(jù)庫,已經(jīng)成功地創(chuàng)建了test_position、test_uses和test_user_position 三張表。

    利用這兩篇文章中的方法,可以先進(jìn)行Java類的設(shè)計(jì),再自動(dòng)生成配置文件和數(shù)據(jù)庫表,這樣做更見符合OO的設(shè)計(jì)思想,但是如果遇到表與表之間關(guān)系復(fù)雜,可能就不是很適合了。嘿嘿
    posted on 2008-07-31 11:28 綠茶_鄭州 閱讀(1777) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
     
    Copyright © 綠茶_鄭州 Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 亚洲乱码在线观看| 亚洲午夜精品一区二区| aaa毛片视频免费观看| 亚洲va在线va天堂va888www| 国产大片线上免费观看| 草久免费在线观看网站| 少妇中文字幕乱码亚洲影视| 精品免费国产一区二区三区| 精品视频在线免费观看| 亚洲女子高潮不断爆白浆| 亚洲精品亚洲人成人网| 在线观看视频免费国语| 久久免费视频网站| 激情无码亚洲一区二区三区 | 亚洲永久在线观看| 亚洲AV无码专区日韩| 永久黄色免费网站| 美女巨胸喷奶水视频www免费| 国产99在线|亚洲| 亚洲va无码专区国产乱码| 国产免费av片在线无码免费看| 无码日韩精品一区二区免费暖暖 | 两性刺激生活片免费视频| 黄 色一级 成 人网站免费| 亚洲人成人伊人成综合网无码| 亚洲av中文无码乱人伦在线播放| 麻豆成人精品国产免费| 永久免费视频网站在线观看| 免费在线黄色电影| 无套内射无矿码免费看黄| 久久精品国产亚洲AV蜜臀色欲 | 久久久精品国产亚洲成人满18免费网站| 美女裸身网站免费看免费网站| 你懂得的在线观看免费视频| 极品色天使在线婷婷天堂亚洲| 亚洲一区二区三区不卡在线播放| 国产成人无码综合亚洲日韩| 亚洲国产婷婷香蕉久久久久久| 日本免费观看网站| 全免费毛片在线播放| 最刺激黄a大片免费网站|