从hbm文件生成ddl

来源:互联网 发布:广数980tdb编程教程 编辑:程序博客网 时间:2024/04/29 21:47

网上收集整理的一些资料:

1、
<property   name="hibernateProperties"> 
  <props> 
  <prop   key="hibernate.dialect"> 
  org.hibernate.dialect.MySQLDialect 
  </prop> 
  <prop   key="hibernate.hbm2ddl.auto">create</prop> 
  <prop   key="hibernate.show_sql">true</prop> 
  </props> 
</property>

2、
<?xml version="1.0"?>
<project name="Export Schema from *.xml"
         default="schema" basedir=".">
  <!-- Set up properties containing important project directories -->
  <property name="class.root" value="E:/hibernate-3.0/"/>
  <property name="lib.dir" value="${class.root}/lib/"/>
  <!-- Set up the class path for compilation and execution -->
  <path id="class.path">
      <!-- Include our own classes, of course -->
      <!--pathelement location="${class.root}" /-->
      <!-- Include jars in the project library directory -->
      <!--fileset dir="${class.root}/hibernate3/org/hibernate/tool/hbm2ddl"-->
      <fileset dir="${class.root}">
        <include name="*.jar"/>
      </fileset>
      <fileset dir="${lib.dir}">
          <include name="*.jar"/>
      </fileset>
  </path>
  <!-- Generate the schemas for all mapping files in our class tree -->
  <target name="schema" description="Generate DB schema from the O/R mapping files">
    <!-- Teach Ant how to use Hibernate's schema generation tool -->
    <taskdef name="schemaexport"
             classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
             classpathref="class.path"/>
    <schemaexport properties="hibernate.properties"
                  quiet="no" text="no" drop="no" out_put="/schema.sql" verbose="true">
      <fileset dir="../src">
        <include name="*.hbm.xml"/>
      </fileset>
    </schemaexport>
  </target>
</project>
 
3、 
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<project name="HibernateS"  default="schemaexport"  basedir=".">

  <property name="base.dir" value="." />
  <property name="src.dir" value="./src" />
  <property name="lib.dir" value="./lib/" />
  <property name="classes.dir" value="./classes/" />
  <property name="build.dir" value="." />

  <path id="myclasspath">
      <fileset dir="${lib.dir}">
          <include name="**/*.jar" />
      </fileset>
  </path>
 
  <target name="schemaexport">
    <taskdef name="schemaexport"
        classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
        classpathref="myclasspath"/>

    <schemaexport
        properties="hibernate.properties"
        quiet="no"
        text="true"
        drop="no"
        delimiter=";"
        output="${build.dir}/schema-export.sql">
        <fileset dir="${classes.dir}">
            <include name="**/ParentChild.hbm.xml"/>
        </fileset>
    </schemaexport>
  </target>
 
</project>

4、直接用语句生成:

//import   net.sf.hibernate.tool.hbm2ddl.*; 
  
  Configuration   config=new   Configuration(); 
  config.addClass(some.class); 
  SchemaExport   export=new   SchemaExport(config); 
  export.setOutputFile("some.sql"); 
  export.create(true,true);
 
 
public static void main(String[] args) throws Exception {

     Configuration cfg = new Configuration()
                 .addClass(Record.class);
                 //.addClass(Record.class);
                 //.addClass(XXX.class);
                 //.addClass(YYY.class);如果要生成多个java类的DDL可以修改
     SessionFactory sf = cfg.buildSessionFactory();
     new SchemaExport(cfg).create(true, true);

     ... ....

}

5、用ant来执行hibernate所自带sechmaExport的工具

既然在eclipse环境下工作,最好所有相关的工作都能在它里面完成。hibernate自带了一个工具schemaExport,可以让你从map文件产生数据库的ddl。
在eclipse project中新建一个目录,schemae,然后新建两个文件。schemae.xml,schema.properties。

schema.properties的内容:

neededClassPath=c:/hibernate-2.1.4/hibernate-2.1/hibernate2.jar;c:/hibernate-2.1.4/hibernate-2.1/lib/dom4j-1.4.jar;c:/hibernate-2.1.4/hibernate-2.1/lib/commons-collections-2.1.jar;c:/hibernate-2.1.4/hibernate-2.1/lib/commons-logging-1.0.3.jar;c:/hsqldb_1_7_1/hsqldb/lib/hsqldb.jar;D:/eclipse-SDK-2.1.2-win32/eclipse/workspace/fhjsj/bin/;D:/eclipse-SDK-2.1.2-win32/eclipse/workspace/fhjsj/fhjsj/WEB-INF/classes/

schema.xml的内容:

<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<project name="schemaExport"  default="schemaexport"  basedir=".">

  <property file="schemae.properties"/>
 
  <target name="schemaexport">
    <taskdef name="schemaexport"
        classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
        classpath="${neededClassPath}"/>

    <schemaexport
        quiet="no"
        text="true"
        drop="no"
        delimiter=";"
        output="workspace/fhjsj/schemae/schema-export.sql">
        <fileset dir="../fhjsj/WEB-INF/classes/">
            <include name="**/*.hbm.xml"/>
        </fileset>
    </schemaexport>
</target>
</project>

其它的hibernate.properties和mapping 文件根据web程序的规范放在WEB-INF的classes里面。最后产生的ddl文件放在schemae目录下面。 

原创粉丝点击