<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 綠茶_鄭州 閱讀(1780) 評論(0)  編輯  收藏

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


    網站導航:
     
     
    Copyright © 綠茶_鄭州 Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 国产精品亚洲一区二区三区在线观看 | 毛色毛片免费观看| 国产亚洲精品久久久久秋霞| 亚洲爆乳大丰满无码专区| 91免费精品国自产拍在线不卡| 亚洲狠狠狠一区二区三区| 毛片无码免费无码播放| 久久国产精品萌白酱免费| 免费人成在线观看网站视频| 亚洲最大天堂无码精品区| 成人免费视频小说| 99999久久久久久亚洲| 免费A级毛片无码免费视| 亚洲中文字幕一二三四区| 日本免费人成黄页在线观看视频| 亚洲人片在线观看天堂无码| 四虎影视永久免费观看网址| 无套内谢孕妇毛片免费看看| 国产成人精品久久亚洲| 免费无码一区二区三区蜜桃| 亚洲图片在线观看| 一二三四视频在线观看中文版免费 | 亚洲国产成人片在线观看| 国产一精品一AV一免费| 亚洲精品白色在线发布| 成人a视频片在线观看免费| 免费看一级高潮毛片| 亚洲美女又黄又爽在线观看| 亚洲精品国产免费| 亚洲GV天堂GV无码男同| 亚洲日韩中文无码久久| 免费黄色福利视频| 另类图片亚洲校园小说区| 亚洲乱码中文字幕久久孕妇黑人| 国产免费阿v精品视频网址| 亚洲avav天堂av在线网爱情| 国产一卡二卡≡卡四卡免费乱码 | 人妻在线日韩免费视频| 亚洲av无码一区二区三区观看| 国产免费无遮挡精品视频| 日本中文字幕免费高清视频|