posts - 262,  comments - 221,  trackbacks - 0

          根據Informa的API說法,Informa提供了基于數據庫的持久化方式,這些持久化方法是通過Hibernate支持的。首先從Informa的源代碼包的build.xml開始追蹤。關于Informa的持久化,在Informa的zip包下有兩個文件和兩個目錄需要注意:

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


          ★Informa的數據庫構建過程

          在Informa的build.xml文件中,有3個target是和數據庫DDL相關的,它們分別是:
           A.hibernate.initdb:初始化數據庫連接
           B.hibernate.dbscheme.generate:根據用戶的指定生成對應的ddl文件 
           C.hibernate.dbscheme:把hbm文件導出成為ddl文件

          這三個target的執行順序是:
           initdb--->dbscheme.generate--->dbscheme 或者
           initdb--->dbscheme

          其中dbscheme是關鍵,它的主要工作包括:
           A.創建用于保存DDL輸出結果的scripts目錄
           B.定義一個名為schemaexport的Ant task,用于把hbm文件導出為ddl文件
           C.執行schemaexport任務,把classes目錄下的hbm文件export成DDL,并輸出到指定的位置
           D.輸出執行結果信息


          為了盡量避免在Ant腳本中hardcode一些設置值,Ant腳本通過下列語句來引入屬性文件

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

          build.properties文件是build.properties-template文件的runtime版,使用者去掉template后綴,然后根據需要修改,在運行時Ant會自動讀取該屬性文件的內容,然后應用到后面引用到這些屬性的地方。

          Ant中允許自定義任務,這個功能通過taskdef標記來完成

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

          這個task通過Hibernate的內置的SchemaExportTask來完成。我們要給他制定classpath路徑。定義完了任務后,就可以在下面使用它了

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

          這個任務中,properties屬性指定了hibernate.properties文件,output指定了最終ddl的輸出位置。當這個任務執行時,會根據hibernate.properties文件中指定的數據庫連接和方言,在classes目錄下搜索hbm文件,然后在scripts目錄下生成對應的ddl文件。

          而對于
          hibernate.dbscheme.generate這個target,最主要的就是下面這個片段
              <!-- 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下的各個數據庫的配置文件來覆蓋hibernate.properties文件,然后通過antcall target="hibernate.dbscheme"這個task來調用上面講到的生成ddl過程。


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

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


          網站導航:
           
          <2009年12月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          BlogJava熱點博客

          好友博客

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 清流县| 大理市| 黑河市| 大悟县| 军事| 永年县| 哈巴河县| 丽江市| 荔波县| 鄂托克前旗| 开远市| 丰镇市| 五大连池市| 无极县| 朝阳区| 体育| 棋牌| 麻江县| 晋宁县| 南阳市| 涟水县| 道真| 石阡县| 蚌埠市| 丘北县| 宜丰县| 包头市| 静宁县| 黄梅县| 镇赉县| 永顺县| 新巴尔虎左旗| 虹口区| 封丘县| 咸阳市| 博乐市| 兴隆县| 潞城市| 涿鹿县| 永济市| 高邑县|