<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: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 国产视频精品免费| AV片在线观看免费| 亚洲日韩精品一区二区三区无码| 亚洲国产av玩弄放荡人妇| 成年人免费观看视频网站| 国产精品亚洲一区二区麻豆| 日韩视频在线精品视频免费观看| 亚洲欧洲国产精品久久| av免费不卡国产观看| 亚洲人成影院午夜网站| 成年人免费视频观看| 日韩色视频一区二区三区亚洲| 国产最新凸凹视频免费| 日韩精品免费一线在线观看| 国产91精品一区二区麻豆亚洲| 巨胸喷奶水www永久免费| 亚洲AV永久无码精品水牛影视| 99爱视频99爱在线观看免费 | 免费人成年激情视频在线观看| 亚洲av无码一区二区三区四区 | 色片在线免费观看| 亚洲人配人种jizz| 亚洲第一区精品日韩在线播放| 国产高清对白在线观看免费91 | 亚洲国产一二三精品无码| 日本免费高清视频| 亚洲啪啪免费视频| 波多野结衣中文一区二区免费| eeuss影院ss奇兵免费com| 亚洲黄色片在线观看| 日韩精品免费电影| 中文字幕av免费专区| 亚洲精品在线不卡| 国产大片免费观看中文字幕| 男人天堂免费视频| 久久久久亚洲国产| 国产亚洲情侣一区二区无码AV| 18禁成人网站免费观看| 国产亚洲综合久久| 久久久久亚洲AV成人片| 国产无遮挡又黄又爽免费视频|