一 环境配置
需要的包:jsdk,ant,hibernate,hibernate-extension,MiddleGen
二 环境变量设置
一般步骤可参照网上
这里,我是这样使用的
拷贝%hibernate_Home%\lib*.jar到%MiddleGen%\lib
拷贝%hibernate_Home%\hibernate2.jar到%MiddleGen%\lib
拷贝%hibernate-extension_Home%\tools\lib*.jar到%MiddleGen%\lib
拷贝%hibernate-extension_Home%\tools\hibernate-tools.jar到%MiddleGen%\lib
三 MiddleGen的配置
配置目标数据库参数
进入MiddleGen 目录下的\config\database 子目录,根据我们实际采用的数据库打开
对应的配置文件。如这里我们用的是mysql数据库,对应的就是mysql.xml文件。
<property name="database.script.file" value="${src.dir}/sql/${name}-mysql.sql"/>
<property name="database.driver.file" value="${lib.dir}/mysql.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="user"/>
<property name="database.password" value="mypass"/>
<property name="database.schema" value=""/>
<property name="database.catalog" value=""/>
<property name="jboss.datasource.mapping" value="mySQL"/>
3) 修改Build.xml
修改MiddleGen 根目录下的build.xml 文件,此文件是Middlegen-Hibernate 的Ant
构建配置。Middlegen-Hibernate将根据build.xml文件中的具体参数生成数据库表映射
文件。可配置的项目包括:
a) 目标数据库配置文件地址
查找关键字 ”!ENTITY”,得到:
<!DOCTYPE project [
<!ENTITY database SYSTEM
"file:./config/database/hsqldb.xml">
]>
默认情况下,MiddleGen 采用的是hsqldb.xml,将其修改为我们所用的数据
库配置文件(mysql.xml):
<!DOCTYPE project [
<!ENTITY database SYSTEM
"file:./config/database/mysql.xml">
]>
b) Application name
查找:
<property name="name" value="airline"/>
“aireline”是MiddleGen原始配置中默认的 Application Name,将其修改为我们
所希望的名称,如“HibernateSample”:
<property name="name" value="HibernateSample"/>
c) 输出目录
查找关键字“name="build.gen-src.dir"”,得到:
<property name="build.gen-src.dir"
value="${build.dir}/gen-src"/>
修改value="${build.dir}/gen-src"使其指向我们所期望的输出目录,
这里我们修改为:
<property name="build.gen-src.dir"
value="C:\sample"/>
d) 对应代码的Package name
查找关键字“destination”,得到:
<hibernate
destination="${build.gen-src.dir}"
package="${name}.hibernate"
genXDocletTags="false"
genIntergratedCompositeKeys="false"
javaTypeMapper=
"middlegen.plugins.hibernate.HibernateJavaTypeMapper"
/>
可以看到,hibernate 节点package 属性的默认设置实际上是由前面的
Application Name (${name})和“.hibernate”组合而成,根据我们的需要,
将其改为:
<hibernate
destination="${build.gen-src.dir}"
package="org.hibernate.sample"
genXDocletTags="true"
genIntergratedCompositeKeys="false"
javaTypeMapper=
"middlegen.plugins.hibernate.HibernateJavaTypeMapper"
/>
这里还有一个属性genXDocletTags,如果设置为true,则生成的代码将包含
xdoclet tag,这为以后在开发过程中借助xdoclet进行映射调整提供了帮助
至此为止,MiddleGen 已经配置完毕,在MiddleGen 根目录下运行ant,就将出现
MiddleGen的界面:
四 .xml到.java
在得到.xml后,使用ant hbm2java 即可得到相应的java文件
总结:
我第一次运行成功,但是后来则出现类似错误,检查发现在MiddleGen的lib目录下存在多个类似的jar包
(velocity.jar,velocity***.jar),删除旧的jar包后,问题解决
[middlegen] java.lang.IncompatibleClassChangeError
[middlegen] at middlegen.KindFileResourceLoader.getResourceStream(KindFileRe
sourceLoader.java:82)
[middlegen] at org.apache.velocity.Template.process(Template.java:136)
[middlegen] at org.apache.velocity.runtime.resource.ResourceManagerImpl.load
Resource(ResourceManagerImpl.java:466)
[middlegen] at org.apache.velocity.runtime.resource.ResourceManagerImpl.getR
esource(ResourceManagerImpl.java:384)
[middlegen] at org.apache.velocity.runtime.RuntimeInstance.getTemplate(Runti
meInstance.java:814)
[middlegen] at org.apache.velocity.runtime.RuntimeInstance.getTemplate(Runti
meInstance.java:796)
[middlegen] at org.apache.velocity.runtime.VelocimacroFactory.initVelocimacr
o(VelocimacroFactory.java:211)
[middlegen] at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInsta
nce.java:276)
[middlegen] at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInsta
nce.java:441)
[middlegen] at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.ja
va:148)
[middlegen] at middlegen.Plugin.getEngine(Plugin.java:560)
[middlegen] at middlegen.Plugin.generate(Plugin.java:444)
[middlegen] at middlegen.Middlegen.writeSource(Middlegen.java:411)
[middlegen] at middlegen.swing.JMiddlegenFrame$1.actionPerformed(JMiddlegenF
rame.java:72)
[middlegen] at javax.swing.AbstractButton.fireActionPerformed(AbstractButton
.java:1786)
[middlegen] at javax.swing.AbstractButton$ForwardActionEvents.actionPerforme
d(AbstractButton.java:1839)
[middlegen] at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultBut
tonModel.java:420)
[middlegen] at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.
java:258)
[middlegen] at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Basi
cButtonListener.java:245)
[middlegen] at java.awt.Component.processMouseEvent(Component.java:5100)
[middlegen] at java.awt.Component.processEvent(Component.java:4897)
[middlegen] at java.awt.Container.processEvent(Container.java:1569)
[middlegen] at java.awt.Component.dispatchEventImpl(Component.java:3615)
[middlegen] at java.awt.Container.dispatchEventImpl(Container.java:1627)
[middlegen] at java.awt.Component.dispatchEvent(Component.java:3477)
[middlegen] at java.awt.LightweightDispatcher.retargetMouseEvent(Container.j
ava:3483)
[middlegen] at java.awt.LightweightDispatcher.processMouseEvent(Container.ja
va:3198)
[middlegen] at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3
128)
[middlegen] at java.awt.Container.dispatchEventImpl(Container.java:1613)
[middlegen] at java.awt.Window.dispatchEventImpl(Window.java:1606)
[middlegen] at java.awt.Component.dispatchEvent(Component.java:3477)
[middlegen] at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
[middlegen] at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDi
spatchThread.java:201)
[middlegen] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDisp
atchThread.java:151)
[middlegen] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.j
ava:145)
[middlegen] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.j
ava:137)
[middlegen] at java.awt.EventDispatchThread.run(EventDispatchThread.java:100
)