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 阅读(1333) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2009年12月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(21)

随笔分类

随笔档案

BlogJava热点博客

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜