Kava Pava Gava Tava Nava Zava Java

everything about Java
随笔 - 15, 文章 - 0, 评论 - 1, 引用 - 0
数据加载中……

Step by step, using hibernate hbm2ddl to generate schema from annotation

(1) Download & install ant. Running ant with eclipse galilio, the output in console view is very strange.

        i. download
        ii. extract to a folder
        iii. set ANT_HOME and PATH

(2) download 

        i. hibernate-tools (HibernateTools-3.2.4.GA-R200905070146-H18.zip), 
        ii. hibernate-annotation (hibernate-annotations-3.4.0.GA.zip)

(3) build.xml like this:  

<?xml version="1.0" encoding="utf-8"?>
<project name="AutoGen" default="make-schema" basedir=".">
    
<property name="proj.home" value="/path/to/your/project" />
    
<property name="src.dir" value="${proj.home}/src" />
    
<property name="hibernate.cfg" value="${proj.home}/src/hibernate.cfg.xml" />
    
<property name="class.dir" value="${proj.home}/build/classes" />
    
<property name="gen.dir" value="./where/to/put/generated/schema" />
    
<property name="lib.dir" value="./where/to/put/libraries/only/for/hbm2ddl/and/not/for/your/project" />

    
<path id="build-classpath">

        
<fileset dir="${lib.dir}">
            
<include name="*.jar" />
        
</fileset>

        
<pathelement location="${class.dir}" />
    
</path>

    
<target name="make-schema">
        
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="build-classpath" />
        
<hibernatetool destdir="${gen.dir}">
            
<classpath refid="build-classpath" />
            
<annotationconfiguration configurationfile="${hibernate.cfg}" />
            
<hbm2ddl export="false" drop="true" create="true" delimiter=";" format="true" outputfilename="schema.sql"/>
        
</hibernatetool>
    
</target>
</project>

 

First thing, let hbm2ddl use its own libraries, not mixing with the development / production environment. See "Hibernate Tools Reference Guide", chapter 4 "Ant Tools", quote below:

Note

There might be incompatibilities with respect to the Hibernate3.jar bundled with the
tools and your own jar. Thus to avoid any confusion it is recommended to use the
hibernate3.jar and hibernate-annotations.jar bundled with the tools
when you want
to use the Ant tasks. Do not worry about using e.g. Hibernate 3.2 jar's with e.g. a
Hibernate 3.1 project since the output generated will work with previous Hibernate
3 versions. 


So, the next step, copy libraries that we will need into {lib.dir}:

From Library
HibernateTools-3.2.4.GA-R200905070146-H18/plugins/org.hibernate.eclipse_3.2.4.GA-R200905070146-H18/lib/tools

hibernate-tools.jar

freemarker.jar

HibernateTools-3.2.4.GA-R200905070146-H18/plugins/org.hibernate.eclipse_3.2.4.GA-R200905070146-H18/lib/hibernate

hibernate3.jar

commons-logging-1.0.4.jar

dom4j-1.6.1.jar

commons-collections-2.1.1.jar

HibernateTools-3.2.4.GA-R200905070146-H18/plugins/org.hibernate.eclipse_3.2.4.GA-R200905070146-H18/lib/annotations

hibernate-annotations.jar

hibernate-commons-annotations.jar

ejb3-persistence.jar


No JDBC driver here, as we don't create the schema, only generate the DDLs.

Now, at the dir where build.xml is, run "ant" and check the file "schema.sql" in the output directory.

posted on 2009-12-09 17:45 bing 阅读(2093) 评论(0)  编辑  收藏 所属分类: Hibernate


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


网站导航: