<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
    統計
    • 隨筆 - 40
    • 文章 - 3
    • 評論 - 0
    • 引用 - 0

    導航

    常用鏈接

    留言簿(1)

    隨筆檔案(40)

    文章檔案(3)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

     
    接上一篇文章,上篇文章介紹了如何利用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的設計思想,但是如果遇到表與表之間關系復雜,可能就不是很適合了。嘿嘿
    posted on 2008-07-31 11:28 綠茶_鄭州 閱讀(1777) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
     
    Copyright © 綠茶_鄭州 Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 免费播放国产性色生活片| a级午夜毛片免费一区二区| 免费乱码中文字幕网站| 亚洲日韩在线观看免费视频| 国产亚洲婷婷香蕉久久精品| 亚欧在线精品免费观看一区| 亚洲欧美在线x视频| 亚洲国产精品一区第二页| 最近最新中文字幕完整版免费高清| 美女18一级毛片免费看| 无码乱人伦一区二区亚洲| 日本不卡在线观看免费v| 东方aⅴ免费观看久久av| 亚洲高清一区二区三区电影| 国产亚洲人成无码网在线观看| 在线看片韩国免费人成视频| 七次郎成人免费线路视频| 亚洲成综合人影院在院播放| 亚洲精品国产高清嫩草影院 | 69成人免费视频| 香蕉国产在线观看免费| 亚洲最大的视频网站| 亚洲色成人网站WWW永久| 大香人蕉免费视频75| 免费在线中文日本| 美女隐私免费视频看| 亚洲av无码片在线观看| 亚洲中文久久精品无码ww16| 卡1卡2卡3卡4卡5免费视频| 99热免费在线观看| 一级毛片免费观看不收费| 在线观看亚洲AV日韩A∨| 亚洲国产精品久久久久婷婷软件| 免费大黄网站在线观| 日韩av无码成人无码免费| 青青草原1769久久免费播放| 免费在线观看亚洲| 亚洲爆乳成av人在线视菜奈实| 亚洲福利视频网址| 久久久亚洲精品视频| 亚洲av成人一区二区三区在线观看|