注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 IB客座主编(四)美国西蒙公..
 帮助

彻底搞定用Xdoclet生成Hibernate所有配置文件


2006-12-16 17:27:24
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
 
背景:
    本人在用Xdoclet生成hibernate的配置文件、实体映射文件和sql脚本的时候,每次总要出点问题,今日问题再现,我在网上泡了一个多小时,没有一篇很完整,很成功的例子。一怒之下,今天誓死也要啃掉这块硬骨头!呵呵,经过4个小时的努力,问题搞定了。现在写出来,以供和各位网友交流。那些小儿科的配置问题不说了,主要是没时间写了,关键看Xdoclet的ant脚本和实体类的Xdoclet标签怎么写,如果你还不熟悉ant,建议你补充一下ant知识再来看本文。
 
环境描述:
Windows Server 2003 Stand Edition
Java SDK 1.5
Ant-1.6.5
Hibernate 3.2.1ga
MySQL-5.0.27
XDoclet-1.2.3
 
目标:
    利用XDoclet从Java持久化类生成hibernate mapping,hibernate.cfg.xml和mysql数据库脚本。
 
具体步骤:(省略环境配置过程)
 
第一步:先写实体类
 
package com.lavasoft.zfv.domain.zfv.book.entity;
import java.util.Date;
/**
 * File Name:   Book.java
 * Created by:  IntelliJ IDEA.
 * Copyright:   Copyright (c) 2003-2006
 * Author:      leizhimin
 * Modifier:    leizhimin
 * Date Time:   2006-12-16 12:54:50
 * Readme:      图书
 */
/**
 * 图书
 *
 * @hibernate.mapping default-lazy="false"
 * @hibernate.meta attribute="class-description" value="图书"
 * @hibernate.class table="bk_ts"
 */

public class Book {
    private Long id;            //标识
    private String code;        //代码
    private String name;        //名称*
    private String bookman;     //出版社
    private Date pubDate;       //出版日期
    private String author;      //作者
    private String translator;  //译者
    private Double price;       //单价*
    private int amount;         //采购数量*
    private String buyer;       //采购人
    private String assessor;    //审核人
    private Date dateMark;      //操作时间(登记日期)*
    private String operator;    //操作员(录入人)*
    private String sort;        //类别(XXYYZZ格式,对应大中细类别)*
    private String summary;     //摘要(内容简介)
    private String remark;      //备注
    public Book() {
    }
    /**
     * @hibernate.id generator-class="native" column="id"
     * @hibernate.meta attribute="field-description" value="标识"
     */

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    /**
     * @hibernate.property column="dm" type="string" length="20" not-null="false"
     * @hibernate.meta attribute="field-description" value="代码"
     */
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    /**
     * @hibernate.property column="mc" type="string" length="100" not-null="true"
     * @hibernate.meta attribute="field-description" value="名称"
     */

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @hibernate.property column="cbs" type="string" length="24"  not-null="false"
     * @hibernate.meta attribute="field-description" value="出版社"
     */
    public String getBookman() {
        return bookman;
    }
    public void setBookman(String bookman) {
        this.bookman = bookman;
    }
    /**
     * @hibernate.property column="cbrq" type="timestamp" not-null="false"
     * @hibernate.meta attribute="field-description" value="出版日期"
     */

    public Date getPubDate() {
        return pubDate;
    }
    public void setPubDate(Date pubDate) {
        this.pubDate = pubDate;
    }
    /**
     * @hibernate.property column="zz" type="string" length="24" not-null="false"
     * @hibernate.meta attribute="field-description" value="作者"
     */

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    /**
     * @hibernate.property column="yz" type="string" length="24" not-null="false"
     * @hibernate.meta attribute="field-description" value="译者"
     */
    public String getTranslator() {
        return translator;
    }
    public void setTranslator(String translator) {
        this.translator = translator;
    }
    /**
     * @hibernate.property column="dj" type="big_decimal" precision="19" scale="6"  not-null="true"
     * @hibernate.meta attribute="field-description" value="单价"
     */
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    /**
     * @hibernate.property column="sl" type="int" not-null="true"
     * @hibernate.meta attribute="field-description" value="数量"
     */

    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    /**
     * @hibernate.property column="cgr" type="string" length="12" not-null="false"
     * @hibernate.meta attribute="field-description" value="采购人"
     */

    public String getBuyer() {
        return buyer;
    }
    public void setBuyer(String buyer) {
        this.buyer = buyer;
    }
    /**
     * @hibernate.property column="shr" type="string" length="12" not-null="false"
     * @hibernate.meta attribute="field-description" value="审核人"
     */

    public String getAssessor() {
        return assessor;
    }
    public void setAssessor(String assessor) {
        this.assessor = assessor;
    }
    /**
     * @hibernate.property column="czsj" type="timestamp" not-null="true"
     * @hibernate.meta attribute="field-description" value="操作时间"
     */

    public Date getDateMark() {
        return dateMark;
    }
    public void setDateMark(Date dateMark) {
        this.dateMark = dateMark;
    }
    /**
     * @hibernate.property column="czy" type="string" length="12" not-null="true"
     * @hibernate.meta attribute="field-description" value="操作员"
     */

    public String getOperator() {
        return operator;
    }
    public void setOperator(String operator) {
        this.operator = operator;
    }
    /**
     * @hibernate.property column="lb" type="string" length="12" not-null="true"
     * @hibernate.meta attribute="field-description" value="类别"
     */

    public String getSort() {
        return sort;
    }
    public void setSort(String sort) {
        this.sort = sort;
    }
    /**
     * @hibernate.property column="zy" type="string" length="600" not-null="false"
     * @hibernate.meta attribute="field-description" value="摘要"
     */

    public String getSummary() {
        return summary;
    }
    public void setSummary(String summary) {
        this.summary = summary;
    }
    /**
     * @hibernate.property column="bz" type="string" length="200" not-null="false"
     * @hibernate.meta attribute="field-description" value="备注"
     */

    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
}

 
第二步:写xdoclet的ant脚本
 
说明,此脚本有四个目标,执行后的结果分别是:
generate-configuration:生成hibernate.cfg.xml
generate-mapping:生成hibernate实体类映射文件
generate-schema-mysql:生成MySQL数据库脚本
注意:在生成MySQL数据库脚本之前应该先生成生成hibernate实体类映射文件。
 
 
<?xml version="1.0" encoding="gb2312"?>
<project name="xdoclet-hibernate-zfv" default="xdoclet" basedir="../../../../../">
    <property name="xdoclet.lib.dir" value="${basedir}/lib"/>
    <property name="project.lib.dir" value="${basedir}/lib"/>
    <property name="project.src.dir" value="${basedir}/src"/>
    <property name="project.resources.dir" value="${basedir}/doc/dbscript"/>
    <property name="tomcat.lib.dir" value="E:\myserver\zfv-tomcat-5.5.20\common\lib"/>
    <property name="hibernate.cfg.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    <property name="hibernate.cfg.driver" value="com.mysql.jdbc.Driver"/>
    <property name="hibernate.cfg.username" value="zfvims"/>
    <property name="hibernate.cfg.password" value="leizhimin"/>
    <property name="hibernate.cfg.jdbcurl" value="jdbc:mysql://localhost:3306/zfvims"/>
    <property name="hibernate.cfg.showsql" value="true"/>
    <target name="xdoclet">
        <path id="xdoclet.task.classpath">
            <pathelement path="${tomcat.lib.dir}/*.jar"/>
            <fileset dir="${xdoclet.lib.dir}">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${project.lib.dir}">
                <include name="**/*.jar"/>
                <exclude name="**/hibernate2.jar"/>
            </fileset>
        </path>
        <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.task.classpath"
        />
    </target>
    <target name="generate-mapping" depends="xdoclet">
        <hibernatedoclet destdir="${project.src.dir}" verbose="true" force="false">
            <fileset dir="${project.src.dir}">
                <include name="**/domain/**/*.java"/>
            </fileset>
            <hibernate version="3.0" xmlencoding="gb2312"/>
        </hibernatedoclet>
    </target>
    <target name="generate-configuration" depends="xdoclet">
        <hibernatedoclet destdir="${project.src.dir}" verbose="true" force="true">
            <fileset dir="${project.src.dir}">
                <include name="**/domain/**/*.java"/>
            </fileset>
            <hibernatecfg
                destinationFile="hibernate.cfg.xml"
                dialect="${hibernate.cfg.dialect}"
                driver="${hibernate.cfg.driver}"
                username="${hibernate.cfg.username}"
                password="${hibernate.cfg.password}"
                jdbcurl="${hibernate.cfg.jdbcurl}"
                showsql="${hibernate.cfg.showsql}"
                destdir="${project.resources.dir}"
                xmlencoding="gb2312"
                />
        </hibernatedoclet>
    </target>
  <target name="generate-schema-mysql" depends="xdoclet">
        <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
            classpathref="xdoclet.task.classpath"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments true" value="true"/>
        <schemaexport
            quiet="no"
            text="yes"
            drop="no"
            delimiter=";"
            output="${project.resources.dir}/zfvims-mysql-schema.sql">
            <fileset dir="${project.src.dir}">
                <include name="**/domain/**/*.hbm.xml"/>
            </fileset>
        </schemaexport>
    </target>
</project>
 
第三步:生成的结果如下
 
生成的配置文件
 
<?xml version="1.0" encoding="gb2312"?>
<!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>
  <!-- properties -->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
  <property name="use_outer_join">false</property>
   <property name="connection.username">zfvims</property>
   <property name="connection.password">leizhimin</property>
   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="connection.url">jdbc:mysql://localhost:3306/zfvims</property>
  <!-- mapping files -->
  <mapping resource="com/lavasoft/zfv/domain/zfv/book/entity/Book.hbm.xml"/>
  <mapping resource="com/lavasoft/zfv/domain/zfv/book/entity/Reader.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

 
生成的映射文件
 
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
        default-lazy="false"
>
    <class
        name="com.lavasoft.zfv.domain.zfv.book.entity.Book"
        table="bk_ts"
    >
        <meta attribute="class-description">图书</meta>
        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <meta attribute="field-description">标识</meta>
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Book.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>
        <property
            name="code"
            type="string"
            update="true"
            insert="true"
            column="dm"
            length="20"
            not-null="false"
        >
            <meta attribute="field-description">代码</meta>
        </property>
        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="mc"
            length="100"
            not-null="true"
        >
            <meta attribute="field-description">名称</meta>
        </property>
        <property
            name="bookman"
            type="string"
            update="true"
            insert="true"
            column="cbs"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">出版社</meta>
        </property>
        <property
            name="pubDate"
            type="timestamp"
            update="true"
            insert="true"
            column="cbrq"
            not-null="false"
        >
            <meta attribute="field-description">出版日期</meta>
        </property>
        <property
            name="author"
            type="string"
            update="true"
            insert="true"
            column="zz"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">作者</meta>
        </property>
        <property
            name="translator"
            type="string"
            update="true"
            insert="true"
            column="yz"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">译者</meta>
        </property>
        <property
            name="price"
            type="big_decimal"
            precision="19"
            scale="6"
            update="true"
            insert="true"
            column="dj"
            not-null="true"
        >
            <meta attribute="field-description">单价</meta>
        </property>
        <property
            name="amount"
            type="int"
            update="true"
            insert="true"
            column="sl"
            not-null="true"
        >
            <meta attribute="field-description">数量</meta>
        </property>
        <property
            name="buyer"
            type="string"
            update="true"
            insert="true"
            column="cgr"
            length="12"
            not-null="false"
        >
            <meta attribute="field-description">采购人</meta>
        </property>
        <property
            name="assessor"
            type="string"
            update="true"
            insert="true"
            column="shr"
            length="12"
            not-null="false"
        >
            <meta attribute="field-description">审核人</meta>
        </property>
        <property
            name="dateMark"
            type="timestamp"
            update="true"
            insert="true"
            column="czsj"
            not-null="true"
        >
            <meta attribute="field-description">操作时间</meta>
        </property>
        <property
            name="operator"
            type="string"
            update="true"
            insert="true"
            column="czy"
            length="12"
            not-null="true"
        >
            <meta attribute="field-description">操作员</meta>
        </property>
        <property
            name="sort"
            type="string"
            update="true"
            insert="true"
            column="lb"
            length="12"
            not-null="true"
        >
            <meta attribute="field-description">类别</meta>
        </property>
        <property
            name="summary"
            type="string"
            update="true"
            insert="true"
            column="zy"
            length="600"
            not-null="false"
        >
            <meta attribute="field-description">摘要</meta>
        </property>
        <property
            name="remark"
            type="string"
            update="true"
            insert="true"
            column="bz"
            length="200"
            not-null="false"
        >
            <meta attribute="field-description">备注</meta>
        </property>
        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Book.xml
            containing the additional properties and place it in your merge dir.
        -->
    </class>
</hibernate-mapping>

 
生成的数据库脚本
 
    drop table if exists bk_ts;
    create table bk_ts (
        id bigint not null auto_increment comment '标识',
        dm varchar(20) comment '代码',
        mc varchar(100) not null comment '名称',
        cbs varchar(24) comment '出版社',
        cbrq datetime comment '出版日期',
        zz varchar(24) comment '作者',
        yz varchar(24) comment '译者',
        dj numeric(19,6) not null comment '单价',
        sl integer not null comment '数量',
        cgr varchar(12) comment '采购人',
        shr varchar(12) comment '审核人',
        czsj datetime not null comment '操作时间',
        czy varchar(12) not null comment '操作员',
        lb varchar(12) not null comment '类别',
        zy text comment '摘要',
        bz varchar(200) comment '备注',
        primary key (id)
    ) comment='图书' type=InnoDB;

 
概括结合Xdoclet来做hibernate应用会给你带来非常高效率和价值,但是配置脚本和实体标签的写法不容易掌握,需要大量实践才可以。这个例子虽然是针对MySQL的,可是稍作修改(数据库方言),就可以用于Oracle、DB2、Sybase下的Hibernate应用,你可以稍作修改将此脚本植入你的项目中,非常的方便。欢迎和我再次交流!
 
声名:原创作品,未经允许,谢绝转载!
 
 
 
 

本文出自 “熔 岩” 博客,转载请与作者联系!





    文章评论
 
2006-12-16 17:41:41
温馨提示:在不同浏览器上,字体大小的显示的效果差异比较大,阅读时可以在浏览器里设置字体大小。

2006-12-17 11:49:18
hehe,温馨提示果然有用。但为啥你的文章每次都要调大小呢?

2006-12-17 20:39:39
我也不清楚,我每次编辑好好的,但发上去之后再浏览字体有时候会很大或者很小,不知道其他的人有没有遇到同样的问题?

2006-12-17 21:06:55
选择10号字,应该不会有问题呀

2006-12-17 22:18:57
还好我的马桶有Zoom Page的功能。

2006-12-18 08:12:00
我也用的是马桶1.5.3 标准版 build18,我只开启了最简单的功能。 以后试试10号字体。

2006-12-31 22:53:35
用IDEA,我喜欢,更喜欢你的文章,我收藏可否? 放在我的个人论坛里? http://trydone.com/posts/list/93.page
一直都难找到你这样的好文章,先COPY去,还望谅解.不妥之处,跟个贴,必删:)

2006-12-31 23:36:53
说说关于文字大小:原因是文章内容没有设置默认文字大小,只要对 body{font-size:12px.....设置一下就不会因浏览器不同而使文字大小发生变化,这个得跟51cto.com的联系了:)
还有IE其实也有改文字大小的一个最快方法,就是 Ctrl+鼠标中间滚轮,同样可以用于Firebox/Opera/NE.

2008-06-21 17:09:45
我按照你的方法来写ant文件,但是总是生成不了meta 注释。不知道是不是有什么地方需要主意的?谢谢
邮箱 hyl_1234@126.com

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: