xdoclet1.2.3&xdoclet2
用xdoclet 和xdoclet2来产生hiberante map文件的一些区别
xdoclet升级到1.2.3了,文档上说是支持hibernate 3了,实际上,经过今天的检验,发现xdoclet 1.2.3只是支持hibernate 3的map文件了,而生成的hibernate 3的config文件,却还hibernate 2的。
一个简单的例子(就是这个例子: ttp://www.redsaga.com/hibernate-ref/3.1.2/zh-cn/html/tutorial.html#tutorial-firstapp-firstclass,按它上面的介绍,设置好各个目录结构):
package events;
import java.util.Date;
/**
*
* @hibernate.class table="EVENTS"
*/
public class Event {
*
* @hibernate.class table="EVENTS"
*/
public class Event {
private Long id;
private String title;
private Date date;
private Date date;
public Event() {
}
}
/**
* @hibernate.id
* generator-class="uuid.hex"
* type="java.lang.String"
* unsaved-value=""
* column="EVENT_ID"
*/
public Long getId() {
return id;
}
* @hibernate.id
* generator-class="uuid.hex"
* type="java.lang.String"
* unsaved-value=""
* column="EVENT_ID"
*/
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
this.id = id;
}
/**
* @hibernate.property
* column="EVENT_DATE"
* type = "timestamp"
*
*
*/
public Date getDate() {
return date;
}
* @hibernate.property
* column="EVENT_DATE"
* type = "timestamp"
*
*
*/
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
this.date = date;
}
/**
* @hibernate.property
* column="EVENT_TITLE"
* length="20"
*
*/
public String getTitle() {
return title;
}
* @hibernate.property
* column="EVENT_TITLE"
* length="20"
*
*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
this.title = title;
}
}
上面的POJO,已经加上了xdoclet的javadoc tag了,现在用下面的ant文件来产生map和config文件:
<project name="xdoclet" default="hibernatedoclet">
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask">
<classpath>
<fileset dir="E:/workspace/xdoclet-1.2.3/lib" includes="*.jar"/>
</classpath>
</taskdef>
<classpath>
<fileset dir="E:/workspace/xdoclet-1.2.3/lib" includes="*.jar"/>
</classpath>
</taskdef>
<target name="hibernate" depends="compile" description="Generate mapping documents">
<hibernatedoclet
destdir="${targetdir}"
excludedtags="@version,@author,@todo,@see"
force="false"
verbose="true">
<hibernatedoclet
destdir="${targetdir}"
excludedtags="@version,@author,@todo,@see"
force="false"
verbose="true">
<fileset dir="${sourcedir}">
<include name="**/*.java"/>
</fileset>
<include name="**/*.java"/>
</fileset>
<hibernatecfg
dialect="net.sf.hibernate.dialect.xxxxx"
driver="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://xxxxx"
userName="xxxxx"
password="xxxxx"/>
<hibernate version="3.0"/>
dialect="net.sf.hibernate.dialect.xxxxx"
driver="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://xxxxx"
userName="xxxxx"
password="xxxxx"/>
<hibernate version="3.0"/>
</hibernatedoclet>
</target>
</target>
</project>
结果如下:
Event.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="events.Event"
table="EVENTS"
>
>
<class
name="events.Event"
table="EVENTS"
>
<id
name="id"
column="EVENT_ID"
type="java.lang.String"
unsaved-value=""
>
<generator class="uuid.hex">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Event.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
name="id"
column="EVENT_ID"
type="java.lang.String"
unsaved-value=""
>
<generator class="uuid.hex">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Event.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="date"
type="timestamp"
update="true"
insert="true"
column="EVENT_DATE"
/>
name="date"
type="timestamp"
update="true"
insert="true"
column="EVENT_DATE"
/>
<property
name="title"
type="java.lang.String"
update="true"
insert="true"
column="EVENT_TITLE"
length="20"
/>
name="title"
type="java.lang.String"
update="true"
insert="true"
column="EVENT_TITLE"
length="20"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Event.xml
containing the additional properties and place it in your merge dir.
-->
To add non XDoclet property mappings, create a file named
hibernate-properties-Event.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" " http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" " http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- Generated file - Do not edit! -->
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<session-factory>
<!-- properties -->
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="use_outer_join">false</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">aPassword</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://xxx</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="use_outer_join">false</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">aPassword</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://xxx</property>
<!-- mapping files -->
<mapping resource="events/Event.hbm.xml"/>
</session-factory>
<mapping resource="events/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
发现没有,上面的config文件中的DTD版本还是2.0。
对于hibernate3来说,现在多了个选择,用xdoclet2也可以产生map文件,但是config文件好像没有办法。
还是用上面POJO,对应的ant文件如下(注意,简单起见,要把xdoclet2下面的lib目录下的jar文件,全部copy到${basedir}/lib下):
<project name="xdoclet2-plugin" default="hibernatedoclet">
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="hibernatedoclet"
depends="compile"
description="Generate Persistence and form classes">
<taskdef
name="xdoclet"
classname="org.xdoclet.ant.XDocletTask"
classpathref="libraries"
/>
<xdoclet>
<!-- defines the file handled by xdoclet2 -->
<fileset dir="${sourcedir}">
<include name="**/*.java"/>
</fileset>
<!-- defines the processing of a plugin -->
<component
classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
destdir="${targetdir}"
version="3.0"
/>
</xdoclet>
depends="compile"
description="Generate Persistence and form classes">
<taskdef
name="xdoclet"
classname="org.xdoclet.ant.XDocletTask"
classpathref="libraries"
/>
<xdoclet>
<!-- defines the file handled by xdoclet2 -->
<fileset dir="${sourcedir}">
<include name="**/*.java"/>
</fileset>
<!-- defines the processing of a plugin -->
<component
classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
destdir="${targetdir}"
version="3.0"
/>
</xdoclet>
</target>
</project>
结果如下:
Event.hbm.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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">
<hibernate-mapping>
<class table="EVENTS" name="events.Event">
<id type="java.lang.String" column="EVENT_ID" unsaved-value="" access="method" name="id">
<generator class="uuid.hex"/>
</id>
<property name="date" type="timestamp" access="method" column="EVENT_DATE"/>
<property name="title" length="20" access="method" column="EVENT_TITLE"/>
</class>
</hibernate-mapping>
<class table="EVENTS" name="events.Event">
<id type="java.lang.String" column="EVENT_ID" unsaved-value="" access="method" name="id">
<generator class="uuid.hex"/>
</id>
<property name="date" type="timestamp" access="method" column="EVENT_DATE"/>
<property name="title" length="20" access="method" column="EVENT_TITLE"/>
</class>
</hibernate-mapping>
发现用xdoclet1.2.3和xdoclet2产生的map文件还是有些区别的,包括形式和内容,也许那些区别都无所谓,但是毕竟有不同。从形式来看,明星xdoclet2产生的文件好看些,所以,以后,我看还是应该用xdoclet2来产生map文件。config文件,就手写吧。
应用 xdoclet1.2.3和xdoclet2,要注意的最大的一点是:POJO的javadoc tag中的所有@hibernate.collection-xxxxx都已经改变成@hibernate.xxxx了。所以,对已有的POJO应用xdoclet2时,可能要做个简单的替换工作。