posts - 0,comments - 0,trackbacks - 0
jar包:
HibernateTools-3.2.0.beta9a\plugins\org.hibernate.eclipse_3.2.0.beta9a\lib\tools\hibernate-tools.jar

帮助文档:
HibernateTools-3.2.0.beta9a\plugins\org.hibernate.eclipse.help_3.2.0.beta9a\doc.zip\html\tools\ant\en\html_single\index.html

使用工具:
hbm2java
hbm2ddl

hbm2java的属性介绍:
<hbm2ddl
export="true|false" (1)
update="true|false" (2)
drop="true|false" (3)
create="true|false" (4)
outputfilename="filename.ddl" (5)
delimiter=";" (6)
format="true|false" (7)
haltonerror="true|false" (8)
>
(1)

export (default: true): Execute the generated statements against the database

(2)

update(default: false): Try and create an update script representing the "delta" between what is in the database and what the mappings specify. Ignores create/update attributes. (Do *not* use against production databases, no guarantees at all that the proper delta can be generated nor that the underlying database can actually execute the needed operations)

(3)

drop (default: false): Output will contain drop statements for the tables, indices & constraints

(4)

create (default: true): Output will contain create statements for the tables, indices & constraints

(5)

outputfilename (Optional): If specified the statements will be dumped to this file.

(6)

delimiter (default: ";"): What delimter to use to separate statements

(7)

format (default: false): Apply basic formatting to the statements.

(8)

haltonerror (default: false): Halt build process if an error occurs.



hbm2java的属性介绍:
<hbm2java
jdk5="true|false" (1)
ejb3="true|false" (2)
>
(1)

jdk (default: false): Code will contain JDK 5 constructs such as generics and static imports

(2)

ejb3 (default: false): Code will contain EJB 3 features, e.g. using annotations from javax.persistence and org.hibernate.annotations




示例:
build.xml(用ant运行):
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hibernate" default="help" basedir=".">
    <!--源文件目录  -->
    <property name="source.root" value="src"/>
    <!--类文件目录  -->
    <property name="class.root" value="WebRoot/WEB-INF/classes"/>
    <!--库文件目录  -->
    <property name="lib.dir" value="WebRoot/WEB-INF/lib"/>
    <!--ddl存放目录-->
    <property name="schema.dir" value="schema"/>
   
    <!--类路径  -->
    <path id="project.class.path">
        <!--包含自己当地的classes  -->
        <pathelement location="${class.root}"/>
       
        <!--包含库里的jars-->
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>
   
    <target name="help">
        <echo message="利用工具开发hibernate"/>
        <echo message="---------------------"/>
        <echo message="有以下任务供选择:"/>
        <echo message=""/>
        <echo message="generate-code-schema --> 运行hbm2java,hbm2ddl,利用hbm.xml文件生成Jopo类和数据表"/>
        <echo message="generate-code --> 运行hbm2java,利用hbm.xml文件生成Jopo类"/>
        <echo message="generate-schema --> 运行hbm2ddl,利用hbm.xml文件生成数据表"/>
        <echo message="generate-schema2 --> 运行schemaExport,利用hbm.xml文件生成数据表"/>
        <echo message=""/>
    </target>
   
   
    <target name="generate-code-schema" description="用hibernatetool包的HibernateTool利用hbm同时导出JOPO和ddl">
        <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="project.class.path"/>
       
        <hibernatetool destdir="${source.root}">
            <configuration configurationfile="${class.root}/hibernate.cfg.xml">
                <fileset dir="${source.root}/">
                    <include name="**/*.hbm.xml"/>
               <!-- <exclude name="**/*test.hbm.xml"/> -->
                </fileset>
            </configuration>
            <hbm2java/>
        </hibernatetool>
       
        <hibernatetool destdir="${schema.dir}">
            <configuration configurationfile="${class.root}/hibernate.cfg.xml">
                <fileset dir="${source.root}/">
                    <include name="**/*.hbm.xml"/>
               <!-- <exclude name="**/*test.hbm.xml"/> -->
                </fileset>
            </configuration>
            <hbm2ddl outputfilename="db.sql"/>
        </hibernatetool>
     </target>
   
   
    <target name="generate-code" description="用hibernatetool包的HibernateTool利用hbm导出JOPO">
        <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="project.class.path"/>
       
        <hibernatetool destdir="${source.root}">
            <configuration configurationfile="${class.root}/hibernate.cfg.xml">
                <fileset dir="${source.root}/">
                    <include name="**/*.hbm.xml"/>
               <!-- <exclude name="**/*test.hbm.xml"/> -->
                </fileset>
            </configuration>
            <hbm2java/>
        </hibernatetool>
     </target>
   
   
    <target name="generate-schema" description="用hibernatetool包的HibernateTool利用hbm导出ddl">
        <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="project.class.path"/>
       
        <hibernatetool destdir="${schema.dir}">
            <configuration configurationfile="${class.root}/hibernate.cfg.xml">
                <fileset dir="${source.root}/">
                    <include name="**/*.hbm.xml"/>
               <!-- <exclude name="**/*test.hbm.xml"/> -->
                </fileset>
            </configuration>
            <hbm2ddl outputfilename="db.sql"/>
        </hibernatetool>
     </target>
   
   
    <target name="generate-schema2" description="用hibernate包自带的SchemaExport利用hbm文件导出ddl">
          <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="project.class.path"/>
       
        <schemaexport output="${schema.dir}/db.sql" config="${class.root}/hibernate.cfg.xml"
                    quiet="no" text="no" drop="no" delimiter=";">
            <fileset dir="${source.root}/cn/hibernate/or">
                <include name="**/*.hbm.xml"/>
            </fileset>           
         </schemaexport>
     </target>
</project>

posted on 2007-06-28 15:16 lyc 阅读(1428) 评论(0)  编辑  收藏 所属分类: Hibernate

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


网站导航: