Hibernate-Extension和Middlegen-Hibernate
参考
Hibernate 开发指南
V1.0
作者:夏昕 xiaxin(at)gmail.com
=========Hibernate学习笔记========
构建Hibernate 基础代码
1、POJO
2、Hibernate 映射文件
数据库表通过Middlegen-Hibernate生成ORM(对象-关系型数据映射)再通过Hibernate-Extension生成POJO,对数据库的操作就可在POJO上完成
1、首先用Middlegen-Hibernate生成ORM
配置目标数据库参数
进入MiddleGen 目录下的\config\database 子目录,根据我们实际采用的数据库打开对应的配置文件,这里我使用的是MSSQL,既打开mssql.xml ,化线部分根据自己的数据库而定
< property name = " database.script.file " value = "" />
< property name = " database.driver.file " value = " ${lib.dir}/jtds-1.1.jar " />
<property name="database.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="database.url" value="jdbc:jtds:sqlserver://localhost:1433/tushu" />
<property name="database.userid" value="sa" />
<property name="database.password" value="dada"/>
<property name="database.schema" value="dbo"/>
<property name="database.catalog" value="tushu"/>
< property name = " jboss.datasource.mapping " value = " MS SQLSERVER " />
修改Build.xml
修改MiddleGen 根目录下的build.xml 文件,此文件是Middlegen-Hibernate 的Ant构建配置。Middlegen-Hibernate将根据build.xml文件中的具体参数生成数据库表映射文件。可配置的项目包括:
a) 目标数据库配置文件地址
查找关键字 ”!ENTITY”,得到(定义数据库使用的xml文件)
DOCTYPE project [
ENTITY database SYSTEM
" file:./config/database/hsqldb.xml " >
] >
ENTITY database SYSTEM
" file:./config/database/hsqldb.xml " >
] >
DOCTYPE project [
ENTITY database SYSTEM "file:./config/database/mssql.xml">
] >
ENTITY database SYSTEM "file:./config/database/mssql.xml">
] >
b) Application name
默认的Application name,可以自定义名称
<
property name
=
"
name
"
value
=
"
HibernateTestDada
"
/>
c) 输出目录
<
property name
=
"
build.gen-src.dir
"
value
=
"
${build.dir}/gen-src
"
/>
<
property name
=
"
build.gen-src.dir
"
value
=
"
D:\sample
"
/>
d) 对应代码的Package name
<
hibernate
destination = " ${build.gen-src.dir} "
package = " ${name}.hibernate "
genXDocletTags = " false "
genIntergratedCompositeKeys = " false "
javaTypeMapper =
" middlegen.plugins.hibernate.HibernateJavaTypeMapper "
/>
destination = " ${build.gen-src.dir} "
package = " ${name}.hibernate "
genXDocletTags = " false "
genIntergratedCompositeKeys = " false "
javaTypeMapper =
" middlegen.plugins.hibernate.HibernateJavaTypeMapper "
/>
<
hibernate
destination = " ${build.gen-src.dir} "
package ="derek.hibernate.test"
genXDocletTags ="true"
genIntergratedCompositeKeys = " false "
javaTypeMapper = " middlegen.plugins.hibernate.HibernateJavaTypeMapper "
/>
destination = " ${build.gen-src.dir} "
package ="derek.hibernate.test"
genXDocletTags ="true"
genIntergratedCompositeKeys = " false "
javaTypeMapper = " middlegen.plugins.hibernate.HibernateJavaTypeMapper "
/>
这里还有一个属性genXDocletTags,如果设置为true,则生成的代码将包含xdoclet tag,这为以后在开发过程中借助xdoclet进行映射调整提供了帮助。
其中原书作者强调如果是SQLSERVER还要删除以下划线内容,否则报错找不到数据库,但是在我测试过程中发现如果去了会报找不到数据库的错误,所以我没有删除,并且在前面“ 配置目标数据库参数”中设置了这两个参数的属性。
<
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}"
>
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}"
>
2、Hibernate Extension生成POJO
hbm2java.bat根据映射文件生成对应的POJO
打开tools\bin\setenv.bat 文件,根据实际情况修改
其中遇到一个问题,原本应该在CP变量中加%CLASSPATH%,但是由于我装过xmlspy,所以当我执行hbm2java时,提示找不到类,其中提示中出现Files\Altova\xmlspy\XMLSpyInterface.jar,但此包我并没加,包括在%CLASSPATH%中也没设置,但他出现了,于是我将%CLASSPATH%从CP中去除,并手工的加入%CLASSPATH%中定义的路径,问题解决!!!
@echo off
rem -------------------------------------------------------------------
rem Setup environment for hibernate tools
rem -------------------------------------------------------------------
set JDBC_DRIVER = D:\HibernateTools\hibernate - extensions - 2.1 . 3 \tools\lib\jtds - 1.1 .jar
set HIBERNATE_HOME = D:\hibernate - 2.1
set HIBERNATETOOLS_HOME = D:\HibernateTools\hibernate - extensions - 2.1 . 3 \tools
echo HIBERNATETOOLS_HOME set to % HIBERNATETOOLS_HOME %
if " %HIBERNATE_HOME% " == "" goto noHIBERNATEHome
set CORELIB =% HIBERNATE_HOME % \lib
set LIB =% HIBERNATETOOLS_HOME % \lib
set PROPS =% HIBERNATE_HOME % \src
set CP =% JDBC_DRIVER % ; % PROPS % ; % HIBERNATE_HOME % \hibernate2.jar; % CORELIB % ; % CORELIB % \commons - logging - 1.0 . 4 .jar; % CORELIB % \commons - lang - 1.0 . 1 .jar; % CORELIB % \cglib - full - 2.0 . 2 .jar; % CORELIB % \dom4j - 1.4 .jar; % CORELIB % \odmg - 3.0 .jar; % CORELIB % \xml - apis.jar; % CORELIB % \xerces - 2.4 . 0 .jar; % CORELIB % \xalan - 2.4 . 0 .jar; % LIB % \jdom.jar; % CORELIB % \commons - collections - 2.1 . 1 .jar; % HIBERNATETOOLS_HOME % \hibernate - tools.jar;.;C:\j2sdk1. 4.2 \lib;C:\j2sdk1. 4.2 \lib\tools.jar;C:\j2sdk1. 4.2 \lib\dt.jar;
if not " %HIBERNATE_HOME% " == "" goto end
:noHIBERNATEHome
echo HIBERNATE_HOME is not set . Please set HIBERNATE_HOME.
goto end
:end
rem -------------------------------------------------------------------
rem Setup environment for hibernate tools
rem -------------------------------------------------------------------
set JDBC_DRIVER = D:\HibernateTools\hibernate - extensions - 2.1 . 3 \tools\lib\jtds - 1.1 .jar
set HIBERNATE_HOME = D:\hibernate - 2.1
set HIBERNATETOOLS_HOME = D:\HibernateTools\hibernate - extensions - 2.1 . 3 \tools
echo HIBERNATETOOLS_HOME set to % HIBERNATETOOLS_HOME %
if " %HIBERNATE_HOME% " == "" goto noHIBERNATEHome
set CORELIB =% HIBERNATE_HOME % \lib
set LIB =% HIBERNATETOOLS_HOME % \lib
set PROPS =% HIBERNATE_HOME % \src
set CP =% JDBC_DRIVER % ; % PROPS % ; % HIBERNATE_HOME % \hibernate2.jar; % CORELIB % ; % CORELIB % \commons - logging - 1.0 . 4 .jar; % CORELIB % \commons - lang - 1.0 . 1 .jar; % CORELIB % \cglib - full - 2.0 . 2 .jar; % CORELIB % \dom4j - 1.4 .jar; % CORELIB % \odmg - 3.0 .jar; % CORELIB % \xml - apis.jar; % CORELIB % \xerces - 2.4 . 0 .jar; % CORELIB % \xalan - 2.4 . 0 .jar; % LIB % \jdom.jar; % CORELIB % \commons - collections - 2.1 . 1 .jar; % HIBERNATETOOLS_HOME % \hibernate - tools.jar;.;C:\j2sdk1. 4.2 \lib;C:\j2sdk1. 4.2 \lib\tools.jar;C:\j2sdk1. 4.2 \lib\dt.jar;
if not " %HIBERNATE_HOME% " == "" goto end
:noHIBERNATEHome
echo HIBERNATE_HOME is not set . Please set HIBERNATE_HOME.
goto end
:end
使用hbm2java,根据MiddleGen生成的映射文件生成Java 代码:
打开Command Window,在tools\bin目录下执行:
hbm2java D:\sample\derek\hibernate\test\*.xml --output=d:\sample\
即可生成对应的POJO。生成的POJO 保存在我指定的输出目录下(d:\sample)
-----------------------------------------------
详细内容可见
Hibernate 开发指南
V1.0
作者:夏昕 xiaxin(at)gmail.com
-----------------------------------------------