背景:
    本人在用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实体类映射文件。
 
 

   
   
   
   
   
   
   
   
   
   
   
   
       
           
           
               
           

           
               
               
           

       
                    classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.task.classpath"
        />
   
   
       
           
               
           

           
       
   
   
       
           
               
           

                            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"
                />
       
   
 
                    classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
            classpathref="xdoclet.task.classpath"/>
       
       
       
                    quiet="no"
            text="yes"
            drop="no"
            delimiter=";"
            output="${project.resources.dir}/zfvims-mysql-schema.sql">
           
               
           

       
   
 
第三步:生成的结果如下
 
生成的配置文件
 

[url]http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd[/url] ">
 
 
  
  org.hibernate.dialect.MySQLDialect
  true
  false
   zfvims
   leizhimin
   com.mysql.jdbc.Driver
   jdbc:mysql://localhost:3306/zfvims
  
  
  
 

 
生成的映射文件
 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "
[url]http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd[/url] ">
        default-lazy="false"
>
            name="com.lavasoft.zfv.domain.zfv.book.entity.Book"
        table="bk_ts"
    >
        图书
                    name="id"
            column="id"
            type="java.lang.Long"
        >
            标识
           
             
           

       
                    name="code"
            type="string"
            update="true"
            insert="true"
            column="dm"
            length="20"
            not-null="false"
        >
            代码
       
                    name="name"
            type="string"
            update="true"
            insert="true"
            column="mc"
            length="100"
            not-null="true"
        >
            名称
       
                    name="bookman"
            type="string"
            update="true"
            insert="true"
            column="cbs"
            length="24"
            not-null="false"
        >
            出版社
       
                    name="pubDate"
            type="timestamp"
            update="true"
            insert="true"
            column="cbrq"
            not-null="false"
        >
            出版日期
       
                    name="author"
            type="string"
            update="true"
            insert="true"
            column="zz"
            length="24"
            not-null="false"
        >
            作者
       
                    name="translator"
            type="string"
            update="true"
            insert="true"
            column="yz"
            length="24"
            not-null="false"
        >
            译者
       
                    name="price"
            type="big_decimal"
            precision="19"
            scale="6"
            update="true"
            insert="true"
            column="dj"
            not-null="true"
        >
            单价
       
                    name="amount"
            type="int"
            update="true"
            insert="true"
            column="sl"
            not-null="true"
        >
            数量
       
                    name="buyer"
            type="string"
            update="true"
            insert="true"
            column="cgr"
            length="12"
            not-null="false"
        >
            采购人
       
                    name="assessor"
            type="string"
            update="true"
            insert="true"
            column="shr"
            length="12"
            not-null="false"
        >
            审核人
       
                    name="dateMark"
            type="timestamp"
            update="true"
            insert="true"
            column="czsj"
            not-null="true"
        >
            操作时间
       
                    name="operator"
            type="string"
            update="true"
            insert="true"
            column="czy"
            length="12"
            not-null="true"
        >
            操作员
       
                    name="sort"
            type="string"
            update="true"
            insert="true"
            column="lb"
            length="12"
            not-null="true"
        >
            类别
       
                    name="summary"
            type="string"
            update="true"
            insert="true"
            column="zy"
            length="600"
            not-null="false"
        >
            摘要
       
                    name="remark"
            type="string"
            update="true"
            insert="true"
            column="bz"
            length="200"
            not-null="false"
        >
            备注
       
       
   

 
生成的数据库脚本
 
    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应用,你可以稍作修改将此脚本植入你的项目中,非常的方便。欢迎和我再次交流!
 
声名:原创作品,未经允许,谢绝转载!