posts - 262,  comments - 221,  trackbacks - 0

          根據(jù)Informa的API說(shuō)法,Informa提供了基于數(shù)據(jù)庫(kù)的持久化方式,這些持久化方法是通過(guò)Hibernate支持的。首先從Informa的源代碼包的build.xml開(kāi)始追蹤。關(guān)于Informa的持久化,在Informa的zip包下有兩個(gè)文件和兩個(gè)目錄需要注意:

           A.build.xml:Ant腳本文件
           B.build.properties-template:Ant腳本文件需要用到的屬性文件模板
           C.config目錄:包含了hibernate.properties文件模板
           D.sql目錄:其下的schema目錄包含了hsqldb,mysql,postgres數(shù)據(jù)庫(kù)的ddl和屬性文件


          ★Informa的數(shù)據(jù)庫(kù)構(gòu)建過(guò)程

          在Informa的build.xml文件中,有3個(gè)target是和數(shù)據(jù)庫(kù)DDL相關(guān)的,它們分別是:
           A.hibernate.initdb:初始化數(shù)據(jù)庫(kù)連接
           B.hibernate.dbscheme.generate:根據(jù)用戶的指定生成對(duì)應(yīng)的ddl文件 
           C.hibernate.dbscheme:把hbm文件導(dǎo)出成為ddl文件

          這三個(gè)target的執(zhí)行順序是:
           initdb--->dbscheme.generate--->dbscheme 或者
           initdb--->dbscheme

          其中dbscheme是關(guān)鍵,它的主要工作包括:
           A.創(chuàng)建用于保存DDL輸出結(jié)果的scripts目錄
           B.定義一個(gè)名為schemaexport的Ant task,用于把hbm文件導(dǎo)出為ddl文件
           C.執(zhí)行schemaexport任務(wù),把classes目錄下的hbm文件export成DDL,并輸出到指定的位置
           D.輸出執(zhí)行結(jié)果信息


          為了盡量避免在Ant腳本中hardcode一些設(shè)置值,Ant腳本通過(guò)下列語(yǔ)句來(lái)引入屬性文件

            <!-- Read in (optional) external file with local property settings -->
            
          <property file="build.properties"/>

          build.properties文件是build.properties-template文件的runtime版,使用者去掉template后綴,然后根據(jù)需要修改,在運(yùn)行時(shí)Ant會(huì)自動(dòng)讀取該屬性文件的內(nèi)容,然后應(yīng)用到后面引用到這些屬性的地方。

          Ant中允許自定義任務(wù),這個(gè)功能通過(guò)taskdef標(biāo)記來(lái)完成

              <taskdef name="schemaexport"
                       classname
          ="org.hibernate.tool.hbm2ddl.SchemaExportTask">
                
          <classpath>
                  
          <pathelement location="${build.classes}"/>
                  
          <path refid="hibernate.classpath"/>
                
          </classpath>
              
          </taskdef>

          這個(gè)task通過(guò)Hibernate的內(nèi)置的SchemaExportTask來(lái)完成。我們要給他制定classpath路徑。定義完了任務(wù)后,就可以在下面使用它了

              <schemaexport properties="${hibernate.prop.file}"
                            quiet
          ="yes"
                            drop
          ="no"
                            text
          ="yes"
                            delimiter
          =";"
                            output
          ="${hibernate.schema.file}">
                
          <fileset refid="hibernate.mapping.files"/>
              
          </schemaexport>

          這個(gè)任務(wù)中,properties屬性指定了hibernate.properties文件,output指定了最終ddl的輸出位置。當(dāng)這個(gè)任務(wù)執(zhí)行時(shí),會(huì)根據(jù)hibernate.properties文件中指定的數(shù)據(jù)庫(kù)連接和方言,在classes目錄下搜索hbm文件,然后在scripts目錄下生成對(duì)應(yīng)的ddl文件。

          而對(duì)于
          hibernate.dbscheme.generate這個(gè)target,最主要的就是下面這個(gè)片段
              <!-- Hypersonic SQL: Replace hibernate settings with dialect specific -->
              
          <copy file="sql/schema/hsqldb/hsqldb-dialect.properties" 
                    tofile
          ="${build.classes}/hibernate.properties" overwrite="true"/>

              
          <antcall target="hibernate.dbscheme">
                
          <param name="hibernate.schema.file" value="sql/schema/hsqldb/hsqldb-hibernate.ddl"/>
              
          </antcall>
              
              
          <!-- MySQL: Replace hibernate settings with dialect specific -->
              
          <copy file="sql/schema/mysql/mysql-dialect.properties" 
                    tofile
          ="${build.classes}/hibernate.properties" overwrite="true"/>

              
          <antcall target="hibernate.dbscheme">
                
          <param name="hibernate.schema.file" value="sql/schema/mysql/mysql-hibernate.ddl"/>
              
          </antcall>
              
              
          <!-- PostgreSQL: Replace hibernate settings with dialect specific -->
              
          <copy file="sql/schema/postgres/postgres-dialect.properties" 
                    tofile
          ="${build.classes}/hibernate.properties" overwrite="true"/>

              
          <antcall target="hibernate.dbscheme">
                
          <param name="hibernate.schema.file" value="sql/schema/postgres/postgres-hibernate.ddl"/>
              
          </antcall>

          它使用sql/scheme下的各個(gè)數(shù)據(jù)庫(kù)的配置文件來(lái)覆蓋hibernate.properties文件,然后通過(guò)antcall target="hibernate.dbscheme"這個(gè)task來(lái)調(diào)用上面講到的生成ddl過(guò)程。


          -------------------------------------------------------------
          生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
          posted on 2009-12-23 10:45 Paul Lin 閱讀(1345) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          <2009年12月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(21)

          隨筆分類(lèi)

          隨筆檔案

          BlogJava熱點(diǎn)博客

          好友博客

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 邮箱| 和龙市| 威信县| 乌鲁木齐县| 咸丰县| 天全县| 澄迈县| 大港区| 乐山市| 德安县| 河南省| 太白县| 循化| 福州市| 杭州市| 固镇县| 四平市| 栾川县| 长乐市| 广平县| 敦化市| 洛扎县| 新宁县| 论坛| 紫阳县| 高邮市| 临汾市| 黑水县| 灵丘县| 大英县| 丰原市| 漳浦县| 湘潭市| 唐海县| 上饶县| 汕头市| 北海市| 林周县| 商洛市| 云阳县| 黄陵县|