本文所用数据库版本为MySQL5.0.15、Oracle10G、MSSQLServer2000。
第一步:准备工作
下载Middlegen-Hibernate-r5、hibernate-extensions-2.1.3,并解压至项目的lib目录下。把数据库驱动拷贝至“Middlegen-Hibernate-r5/lib”目录下。
第二步:配置MiddlenGen所需要的目标数据库参数
修改在“Middlegen-Hibernate-r5/config/database”目录下的配置文件。
1、 Hibernate+MySQL配置
<property name="database.script.file" value="${src.dir}/sql/${name}-mysql.sql"/>
<property name="database.driver.file" value="${lib.dir}/mysql-connector-java-3.1.12-bin.jar"/>
<property name="database.driver.classpath" value="${database.driver.file}"/>
<property name="database.driver" value="org.gjt.mm.mysql.Driver"/>
<property name="database.url" value="jdbc:mysql://localhost/sample"/>
<property name="database.userid" value="root"/>
<property name="database.password" value="root"/>
<property name="database.schema" value=""/>
<property name="database.catalog" value=""/>
<property name="jboss.datasource.mapping" value="mySQL"/>
2、 Hibernate+Oracle10G配置
<property name="database.script.file" value="${src.dir}/sql/${name}-oracle.sql"/>
<property name="database.driver.file" value="${lib.dir}/ojdbc14.jar"/>
<property name="database.driver.classpath" value="${database.driver.file}"/>
<property name="database.driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="database.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="database.userid" value="SAMPLE"/>
<property name="database.password" value="SAMPLE"/>
<property name="database.schema" value="SAMPLE"/><!—注意此处一定要配置schema-->
<property name="database.catalog" value=""/>
<property name="jboss.datasource.mapping" value="Oracle10g"/>
3、 Hibernate+MSSQLServer2000配置
<property name="database.script.file" value=""/>
<property name="database.driver.file" value="${lib.dir}/mssqlserver.jar"/>
<property name="database.driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<property name="database.url" value="jdbc:microsoft:sqlserver://localhost:1433;database=sample"/>
<property name="database.userid" value="sample"/>
<property name="database.password" value="sample"/>
<property name="database.schema" value="dbo"/><!—注意此处一定要配置schema,且要设置为dbo-->
<property name="database.catalog" value="sample"/><!—注意此处一定要配置catalog -->
<property name="jboss.datasource.mapping" value="MS SQLSERVER2000"/>
另外一种使用JTDS驱动连接SQLServer2000的配置
<property name="database.script.file" value=""/>
<property name="database.driver.file" value="${lib.dir}/jtds-1.2.5.jar"/>
<property name="database.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="database.url" value="jdbc:jtds:sqlserver://localhost:1433/sample"/>
<property name="database.userid" value="sample"/>
<property name="database.password" value="sample"/>
<property name="database.schema" value="dbo"/>
<property name="database.catalog" value="sample"/>
<property name="jboss.datasource.mapping" value="MS SQLSERVER2000"/>
第三步:修改Middlegen的build.xml
修改“Middlegen-Hibernate-r5”根目录下的build.xml文件。
Hibernate2修改如下:
<?xml version="1.0"?>
<!DOCTYPE project [
<!ENTITY database SYSTEM "file:./config/database/mysql.xml"><!—此处修改为对应xml文件-->
]>
<project name="Middlegen Hibernate" default="all" basedir=".">
<!-- project name="Middlegen Hibernate" default="all" basedir="." -->
<property file="${basedir}/build.properties"/>
<property name="name" value="HibernateSample"/><!—此处修改为期望的Application Name-->
<!-- This was added because we were several people (in a course) deploying to same app server>
<property environment="env"/>
<property name="unique.name" value="${name}.${env.COMPUTERNAME}"/-->
<property name="gui" value="true"/>
<property name="unique.name" value="${name}"/>
<property name="appxml.src.file" value="${basedir}/src/application.xml"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="java.src.dir" value="${src.dir}/java"/>
<property name="web.src.dir" value="${src.dir}/web"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="build.java.dir" value="${build.dir}/java"/>
<property name="build.gen-src.dir" value="../../src"/><!—此处修改为期望的输出目录-->
<property name="build.classes.dir" value="${build.dir}/classes"/>
............
<!-- =================================================================== -->
<!-- Run Middlegen -->
<!-- =================================================================== -->
<target
name="middlegen"
description="Run Middlegen"
unless="middlegen.skip"
depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"
>
<mkdir dir="${build.gen-src.dir}"/>
<echo message="Class path = ${basedir}"/>
<taskdef
name="middlegen"
classname="middlegen.MiddlegenTask"
classpathref="lib.class.path"
/>
<middlegen
appname="${name}"
prefsdir="${src.dir}"
gui="${gui}"
databaseurl="${database.url}"
initialContextFactory="${java.naming.factory.initial}"
providerURL="${java.naming.provider.url}"
datasourceJNDIName="${datasource.jndi.name}"
driver="${database.driver}"
username="${database.userid}"
password="${database.password}"
schema="${database.schema}"
catalog="${database.catalog}"
>
............
<!—此处修改为期望的包名,设置为包含XDocletTag -->
<hibernate
destination="${build.gen-src.dir}"
package="org.redsaga.quickstart"
genXDocletTags="true"
genIntergratedCompositeKeys="false"
javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"
/>
</middlegen>
<mkdir dir="${build.classes.dir}"/>
</target>
............
</project>
Hibernate3的设置与修改:
首先修改“Middlegen-Hibernate-r5/middlegen-lib”目录下的middlegen-hibernate-plugin-2.1.jar内的“middlegen/plugins/hibernate/”包路径下的hibernate.vm文件,把
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
改为:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
通过网址:http://sourceforge.net/projects/jboss/files/JBossTools/JBossTools3.0.0.x/HibernateTools-3.2.4.GA-R200903141626-H5.zip/download,下载Hibernate Tools。
下载后获取“plugins/org.hibernate.eclipse_3.2.4.GA-R200903141626-H5/lib/tools”下的freemarker.jar、hibernate-tools.jar,拷贝到“Middlegen-Hibernate-r5/lib”目录下。
获取“”目录下的dom4j-1.6.1.jar、hibernate3.jar拷贝到“Middlegen-Hibernate-r5/lib”目录下。
从网站“http://www.slf4j.org/“上获取slf4j-api-1.5.8.jar和slf4j-jdk14-1.5.8.jar,拷贝到“Middlegen-Hibernate-r5/lib”目录下。
修改build.xml文件
<!DOCTYPE project [
<!ENTITY database SYSTEM "file:./config/database/mysql.xml"><!—此处修改为对应xml文件-->
]>
............
<property name="name" value="HibernateSample"/><!—此处修改为期望的Application Name-->
<!-- This was added because we were several people (in a course) deploying to same app server>
<property environment="env"/>
<property name="unique.name" value="${name}.${env.COMPUTERNAME}"/-->
<property name="gui" value="true"/>
<property name="unique.name" value="${name}"/>
<property name="appxml.src.file" value="${basedir}/src/application.xml"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="java.src.dir" value="${src.dir}/java"/>
<property name="web.src.dir" value="${src.dir}/web"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="build.java.dir" value="${build.dir}/java"/>
<property name="build.gen-src.dir" value="../../src"/><!—此处修改为期望的输出目录-->
<property name="build.classes.dir" value="${build.dir}/classes"/>
............
<target
name="middlegen"
description="Run Middlegen"
unless="middlegen.skip"
depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"
>
<mkdir dir="${build.gen-src.dir}"/>
<echo message="Class path = ${basedir}"/>
<taskdef
name="middlegen"
classname="middlegen.MiddlegenTask"
classpathref="lib.class.path"
/>
<middlegen
appname="${name}"
prefsdir="${src.dir}"
gui="${gui}"
databaseurl="${database.url}"
initialContextFactory="${java.naming.factory.initial}"
providerURL="${java.naming.provider.url}"
datasourceJNDIName="${datasource.jndi.name}"
driver="${database.driver}"
username="${database.userid}"
password="${database.password}"
schema="${database.schema}"
catalog="${database.catalog}"
>
............
<!—此处修改为期望的包名,设置为包含XDocletTag -->
<hibernate
destination="${build.gen-src.dir}"
package="org.redsaga.quickstart"
genXDocletTags="true"
genIntergratedCompositeKeys="false"
javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"
/>
</middlegen>
<mkdir dir="${build.classes.dir}"/>
</target>
............
</project>
第四步:运行build.xml
运行build.xml后出现如下界面:
修改Key generator的方式,点击“Generate”按钮,生成.hbm.xml文件。Hibernate2和Hibernate3生成的文件dtd版本不一样。
第五步:修改Middlegen的build.xml中的path和hbm2java设置
Hibernate2配置如下:
<path id="lib.class.path">
<pathelement path="${database.driver.classpath}"/>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<!-- The middlegen jars -->
<!--fileset dir="${basedir}/.."-->
<fileset dir="${basedir}/middlegen-lib">
<include name="*.jar"/>
</fileset>
<!—增加Hibernate和Hibernate-extensions的类包-->
<pathelement path="../hibernate-2.1/hibernate2.jar"/>
<fileset dir="../hibernate-2.1/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="../hibernate-extensions-2.1.3/tools/hibernate-tools.jar"/>
<fileset dir="../hibernate-extensions-2.1.3/tools/lib">
<include name="*.jar"/>
</fileset>
</path>
Hibernate3配置如下(由于将Hibernate Tools和Hibernate3的jar包拷贝到了lib下,path路径不再需要修改,“Hibernate-extensions“也不再需要。
<target name="hbm2java" description="Generate .java from .hbm files.">
<taskdef
name="hbm2java"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="lib.class.path"
/>
<hbm2java destdir="${build.gen-src.dir}">
<configuration>
<fileset dir="${build.gen-src.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java jdk5="true" />
</hbm2java>
</target>
第六步:在控制台build.xml目录下,运行“ant hbm2java”命令。生成Java代码。