Hibernate XDoclet 在Eclipse中的模版
开发Hibernate很久,最终还是XDoclet提供的帮助最大。能够处理复杂的对象关系,也最符合面向对象的原则。方法1:
OO类图--〉数据库设计--〉MiddleGen (能够处理基本的关联关系--打开XDoclet标签生成开关,但不能处理继承概念,较为遗憾ing)-->在Eclipse手工更新Java类中 的XDoclet标签,然后XDoclet生成Hbm文件。当然了再写个JUnit测试一下关联关系是否正确,必要的Lazy是否标注。
下载Template: http://raibledesigns.com/wiki/Wiki.jsp?page=XDocletEclipse#hibcolidx 非常感谢 MattRaible.
在中文环境中,window xp的字符切换键与Eclipse模版的字符快捷键重合,需要修改之。为了避免麻烦,直接修改Template文件,简单添加@标示符:全文如下:
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<template name="@hibarray" description="@hibernate.array" context="javadoc" enabled="true">@hibernate.array table="" cascade="save-update"</template>
<template name="@hibbag" description="@hibernate.bag" context="javadoc" enabled="true">@hibernate.bag table="" lazy="false" cascade="none" inverse="false"</template>
<template name="@hibclass" description="@hibernate.class" context="javadoc" enabled="true">@hibernate.class table="${enclosing_type}"</template>
<template name="@hibcolelm" description="@hibernate.collection-element" context="javadoc" enabled="true">@hibernate.collection-element column="" type="" length=""</template>
<template name="@hibcolidx" description="@hibernate.collection-index" context="javadoc" enabled="true">@hibernate.collection-index column="" type="" length=""</template>
<template name="@hibcolkey" description="@hibernate.collection-key" context="javadoc" enabled="true">@hibernate.collection-key column="" generator-class="native"</template>
<template name="@hibcolmtm" description="@hibernate.many-to-many" context="javadoc" enabled="true">@hibernate.set name="${enclosing_method}" table="link_table_name_here" cascade="save-update" inverse="true|false" lazy="true"
* @hibernate.collection-key column="${enclosing_type}_ID"
* @hibernate.collection-many-to-many class="relationship_class_the_set_contains" column="relationship_foreign_key"
* @return ${return_type}</template>
<template name="@hibcolotm" description="@hibernate.one-to-many relationship" context="javadoc" enabled="true">@hibernate.set name="${enclosing_method}" table="relationship_table"
* sort="comparator_class" inverse="true|false"
* cascade="save-update" lazy="true"
* @hibernate.collection-key column="${enclosing_type}_ID"
* @hibernate.collection-one-to-many class="relationship_class"
*
* @return ${return_type}</template>
<template name="@hibcomelm" description="@hibernate.collection-composite-element" context="javadoc" enabled="true">@hibernate.collection-composite-element class=""</template>
<template name="@hibcomp" description="@hibernate.component" context="javadoc" enabled="true">@hibernate.component class="component_class_name"</template>
<template name="@hibdisc" description="@hibernate.discriminator" context="javadoc" enabled="true">@hibernate.discriminator column="subclass" type="character"</template>
<template name="@hibid" description="@hibernate.id" context="javadoc" enabled="true">Note: unsaved-value An identifier property value that indicates that an instance
* is newly instantiated (unsaved), distinguishing it from transient instances that
* were saved or loaded in a previous session. If not specified you will get an exception like this:
* another object associated with the session has the same identifier
*
* @hibernate.id generator-class="" type="${return_type}" column="${enclosing_type}_ID"
* unsaved-value="null" length=""
* @return ${return_type}</template>
<template name="@hiblist" description="@hibernate.list" context="javadoc" enabled="true">@hibernate.list table="relationship-table" lazy="false" cascade="none"</template>
<template name="@hibmap" description="@hibernate.map" context="javadoc" enabled="true">@hibernate.map name="${enclosing_method}" table="relationship-table" lazy="false" cascade="none"</template>
<template name="@hibmto" description="@hibernate.many-to-one" context="javadoc" enabled="true">@hibernate.many-to-one column="${return_type}_ID" class="package.${return_type}"
*
* @return ${return_type}
*</template>
<template name="@hiboto" description="@hibernate.one-to-one" context="javadoc" enabled="true">hibernate.one-to-one cascade="none" class="" outer-join="auto"</template>
<template name="@hibprimarr" description="@hibernate.primitive-array" context="javadoc" enabled="true">@hibernate.primitive-array table="" cascade="none"</template>
<template name="@hibprop" description="@hibernate.property" context="javadoc" enabled="true">@hibernate.property name="${enclosing_method}" column="${enclosing_method}" type="${return_type}" not-null="false" unique="false"
*
* @return ${return_type}</template>
<template name="@hibquery" description="@hibernate.query" context="javadoc" enabled="true">@hibernate.query name="" query=""</template>
<template name="@hibset" description="@hibernate.set" context="javadoc" enabled="true">@hibernate.set name="${enclosing_method}" table="relationship_table"
* sort="comparator_class" inverse="true"
* cascade="save-update" lazy="true"</template>
<template name="@hibsubc" description="@hibernate.subclass" context="javadoc" enabled="true">@hibernate.subclass name="" discriminator-value=""</template>
<template name="@hibts" description="@hibernate.timestamp" context="javadoc" enabled="true">@hibernate.timestamp column="${enclosing_method}"
*
* @return ${return_type}</template>
<template name="@hibver" description="@hibernate.version" context="javadoc" enabled="true">@hibernate.version column="${enclosing_method}"
*
* @return ${return_type}</template>
</templates>
使用时:先把XML内容单独保存为文件,然后在Eclipse-->Windows-->Preferences
在Preferences-->Java-->Editor-->Templates 点击Import按钮导入之前已经保存的XML文件。
方法2:
OO类图--〉在Eclipse手工编写属性--〉生成Get/Set方法--〉更新Java类中 的XDoclet标签,然后XDoclet生成Hbm文件。当然了再写个JUnit测试一下关联关系是否正确,必要的Lazy是否标注。
要求先修改Get方法的模板:源代码编辑器中鼠标右键--〉Source--〉Generate Getters And Setters..
点击打开面板中Code Template链接。
编辑Getter方法模板:
/**
* @hibernate.property name="${bare_field_name}" column="${field}" type="${field_type}" not-null="false" unique="false" length="128"
* @return Returns the ${bare_field_name}.
*/
然后生成代码,手工微调部分属性。也能够节约大量时间。
注意:在编写Java POJO类时,java属性用完整的带包名的类,例如:
/**
* @author jdyao
* @hibernate.class table="respri"
* @version
*/
public class Resource implements Serializable {
private static final long serialVersionUID = 1505581058179605003L;
private java.lang.String guid;
private java.lang.String context;
public Resource () {
}
/**
* @return java.lang.String
* @hibernate.property name="context" type="java.lang.String"
* length="128"
*
*/
public java.lang.String getContext() {
return context;
}
public void setContext(java.lang.String context) {
this.context = context;
}
/**
* @return java.lang.String
* @hibernate.id generator-class="guid" type="java.lang.String" column="guid"
* unsaved-value="null" length="38"
*/
public java.lang.String getGuid() {
return guid;
}
public void setGuid(java.lang.String guid) {
this.guid = guid;
}
}
原因:XDoclet在生成的时候,如果type="string",有时会出现错误,无法生成Hbm文件,为了避免这个不必要的错误,务必要写全类名。
XDoclet build.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="XDoclet Examples" default="hibernate" basedir=".">
<property name="xdoclet.root.dir" value="${basedir}"/>
<property file="${xdoclet.root.dir}/build.properties"/>
<!-- Include the build-dist properties. Since properties are immutable,
this will not override available properties. You do not have to include
this in your own build file. -->
<property file="build-dist.properties"/>
<!-- See CustomerBean. This is to demonstrate property substitution. -->
<property name="ejb.prefix" value="blah"/>
<!-- =================================================================== -->
<!-- Define the class path -->
<!-- =================================================================== -->
<path id="samples.class.path">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<fileset dir="${samples.lib.dir}">
<include name="*.jar"/>
</fileset>
<fileset dir="${dist.lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<!-- =================================================================== -->
<!-- Initialise -->
<!-- =================================================================== -->
<target name="init">
<tstamp>
<format property="TODAY" pattern="d-MM-yy"/>
</tstamp>
<taskdef
name="xdoclet"
classname="xdoclet.DocletTask"
classpathref="samples.class.path"
/>
<taskdef
name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="samples.class.path"
/>
</target>
<!-- =================================================================== -->
<!-- Prepares the directory structure -->
<!-- =================================================================== -->
<target name="prepare" depends="init">
<mkdir dir="${samples.classes.dir}"/>
<mkdir dir="${samples.gen-src.dir}"/>
<mkdir dir="${samples.meta-inf.dir}"/>
</target>
<!-- =================================================================== -->
<!-- Invoke XDoclet's hibernate -->
<!-- =================================================================== -->
<target name="hibernate" depends="prepare" description="Generate mapping documents (run jar first)">
<echo>+---------------------------------------------------+</echo>
<echo>| |</echo>
<echo>| R U N N I N G H I B E R N A T E D O C L E T |</echo>
<echo>| |</echo>
<echo>+---------------------------------------------------+</echo>
<hibernatedoclet
destdir="${samples.gen-src.dir}"
mergedir="${samples.src.dir}"
excludedtags="@version,@author,@todo,@see"
addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
force="${samples.xdoclet.force}"
verbose="false">
<fileset dir="${samples.java.dir}">
<include name="**/**/*.java"/>
</fileset>
<hibernate version="3.0"/>
</hibernatedoclet>
</target>
<!-- =================================================================== -->
<!-- Clean -->
<!-- =================================================================== -->
<target name="clean">
<delete dir="${samples.dist.dir}"/>
</target>
</project>
build-dist.properties 文件:
# These properties are only used when building the samples expanded from the distribution.
lib.dir = ${xdoclet.root.dir}/lib
dist.lib.dir = ${lib.dir}
samples.dir = ${xdoclet.root.dir}
samples.dist.dir = ${samples.dir}/target
samples.lib.dir = ${samples.dir}/lib
samples.src.dir = ${samples.dir}/src
samples.java.dir = ${samples.src.dir}/java
samples.gen-src.dir = ${samples.dist.dir}/gen-src
samples.meta-inf.dir = ${samples.dist.dir}/meta-inf
samples.web-inf.dir = ${samples.dist.dir}/web-inf
samples.merge.dir = ${samples.src.dir}/merge
samples.classes.dir = ${samples.dist.dir}/classes
samples.web.dir = ${samples.src.dir}/web
samples.xdoclet.force = false
工程目录结构:从XDoclet网站下载该包,解压缩后,把Example目录单独copy出来,把这2个文件放在Example目录下,同时建立lib目录,把XDoclet目录下--〉lib目录下的*.jar拷贝到Example新建立的lib目录下。