浏览 4276 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-02-23
最后修改:2010-02-23
用Xdoclet由POJOt生成hbm文件时不能生成meta注释的问题”中介绍了采用xdoclet1生成hibernate映射文件的一些问题,这里不再提了,有兴趣的朋友可以看一下。
在我的一篇“
在采用xdoclet1生成了带meta标记的注释后问题依然没有断,在使用hibernate的SchemaExportTask生成ddl数据库脚本的时候发现生成的脚本都没有注释,就是字段或者表后面加上comment定义(如:MZ varchar(255) comment '名字', 以mysql为例),查找原因,原来是我用的hibernate版本(hibernate3.2.7)有点高,这时的hibernate开始支持comment标记生成注释了,不再支持meta啦。当然如果替换成hibernate3.1版本的是可以生成注释的。。 这里我不准备使用hibernate3.1,但是xdoclet1不支持@hibernate.comment生成<comment>名字</comment>标记的生成,无奈放弃xdoclet1,使用xdoclet2。 xdoclet2对hibernate的支持非常的好,所有的标签都有了,可以像使用hibernate标记一样使用它们,xdoclet2的hibernateTag参见http://xdoclet.codehaus.org/HibernateTags。下面就是用xdoclet2演示一下生成hibernate3映射文件进而生成ddl数据库schema脚本。开发环境如下:
项目目录结构如下: ![]() 引用的jar列表如下: ![]() 1.新建一个Person实体类代码如下: package com.flysnow.domain.entity; import java.io.Serializable; /** * @author 飞雪无情 * @since:2010-2-20 */ /** * @hibernate.mapping default-lazy="false" * @hibernate.class table="t_person" * @hibernate.comment 人 */ public class Person implements Serializable { private static final long serialVersionUID = 6422096732289758030L; private Long id;//标识 private String name;//名字 private Integer age;//年龄 /** * @hibernate.id generator-class="native" * @hibernate.column name="ID" comment="标识" */ public Long getId() { return id; } public void setId(Long id) { this.id = id; } /** * @hibernate.property type="string" length="50" not-null="true" * @hibernate.column name="MZ"comment="名字" */ public String getName() { return name; } public void setName(String name) { this.name = name; } /** * @hibernate.property type="integer" not-null="true" * @hibernate.column name="NL" comment="年龄" */ public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } 这个类很简单,很好理解,其中 @hibernate.comment 人就是xdoclet2中对实体类的注释标记,而 @hibernate.column name="ID" comment="标识"则是对字段的注释标记,生成dll数据库脚本就是对表和列的注释说明。 2.新建ant脚本build.xml <?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== 2010-2-20 下午02:45:02 project description 飞雪无情 ====================================================================== --> <project name="project" default="generator-schema" basedir="."> <path id="build.lib"> <fileset dir="${basedir}/lib"> <include name="**/*.jar"/> </fileset> <fileset dir="${basedir}/lib/xdoclet"> <include name="**/*.jar"/> </fileset> <pathelement location="${basedir}/build/class"/> </path> <target name="complie" description="编译"> <javac srcdir="${basedir}/src" destdir="${basedir}/build/class"> <classpath refid="build.lib"></classpath> </javac> </target> <target name="generator-mapping" description="生成Mapping文件"> <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="build.lib"></taskdef> <echo message="生成Mapping文件..."></echo> <xdoclet> <fileset dir="${basedir}/src"> <include name="**/entity/*.java"/> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" destdir="${basedir}/src" version="3.0" encoding="UTF-8" force="true"/> </xdoclet> </target> <target name="generator-schema" depends="complie" description="生成schema文件"> <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="build.lib"></taskdef> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.use_sql_comments" value="true"/> <echo message="生成schema文件..."></echo> <schemaexport quiet="no" text="yes" drop="no" delimiter=";" output="${basedir}/src/ant-schema.sql"> <fileset dir="${basedir}/src"> <include name="**/entity/*.hbm.xml"/> </fileset> </schemaexport> </target> </project> 下面对ant脚本解释一下,这段 <path id="build.lib"> <fileset dir="${basedir}/lib"> <include name="**/*.jar"/> </fileset> <fileset dir="${basedir}/lib/xdoclet"> <include name="**/*.jar"/> </fileset> <pathelement location="${basedir}/build/class"/> </path>是定义一个classpath,这里一定记得把编译后的实体的class文件加入到classp里面否则在生成ddl的数据会报 引用 Schema text failed: Could not parse mapping document from file E:\workspace\ant\src\com\flysnow\domain\entity\Person.hbm.xml 的异常。这问异常困扰了我很久。
下面这段是一个编译任务,相信大家都理解。 <target name="complie" description="编译"> <javac srcdir="${basedir}/src" destdir="${basedir}/build/class"> <classpath refid="build.lib"></classpath> </javac> </target> 下面的就是生成Mapping的任务了,比较主要的就是对Task的定义 <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="build.lib"></taskdef>以及xdoclet的使用 <xdoclet> <fileset dir="${basedir}/src"> <include name="**/entity/*.java"/> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" destdir="${basedir}/src" version="3.0" encoding="UTF-8" force="true"/> </xdoclet>其中比较新的就是component标记的使用了,这里其实就是以插件的形式引入对生成hibernate Mapping文件的支持,其主要属性有
其他的一些属性可以参考http://xdoclet.codehaus.org/HibernateMappingPlugin 最后就是生成ddl数据库脚本的任务,任务定义也很简单 <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="build.lib"></taskdef>关键的几个需要配置的属性 如下,这些是为SchemaExportTask任务提供的。 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.use_sql_comments" value="true"/> 下面是该任务的一些常用属性:
要注意的地方是text属性,如果你不想生成ddl数据库脚本的同时还导入到数据库,就把属性值设为yes,否则你就必须得为hibernate配置数据库连接的属性(连接url,用户名密码等),如若不然就会抛 引用 java.lang.UnsupportedOperationException: The user must supply a JDBC connection 异常
3.测试结果 现在项目已经完成了,我们打开cmd,cd进入到build.xml文件所在的目录,运行ant generator-mapping会生成Mapping文件,运行ant generator-schema就会生成ddl数据库schema脚本。。当然不要忘记创建ANT_HOME环境变量并且把ant加入到path环境变量中..附件中有完整的工程。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-04-20
最后修改:2010-04-20
楼主很细心啊,不错
|
|
返回顶楼 | |
发表时间:2010-04-21
endell 写道 楼主很细心啊,不错
呵呵。。都是实际中遇到的一些问题。。写出来给大家分享。。 |
|
返回顶楼 | |
发表时间:2010-12-27
你有没有发现在生成的数据库脚本里面 字段的长度不对?你设定的是50 生成的确是255
|
|
返回顶楼 | |