隨筆-57  評論-202  文章-17  trackbacks-0
                在數據庫建立后,可以用Middlegen生成每個表對應的hbm.xml文件,并用hibernate-extensions生成對應的Java類。在后期開發時,如果要修改表,修改完后再走一遍這樣的流程,那就顯得有些繁瑣。用XDoclet可以直接根據修改后的Java類生成對應的hbm.xml文件,這樣可以減少維護的工作量。下面是我的一個簡單例子。
                我使用的數據庫是MySQL 4.1.11,在數據庫test中有這樣一個用戶表:

          1CREATE TABLE IF NOT EXISTS User
          2(
          3   Guid                           INT                            NOT NULL AUTO_INCREMENT,
          4   Account                        VARCHAR(64)                    NOT NULL,
          5   Password                       VARCHAR(16)                    NOT NULL,
          6   Email                          VARCHAR(128)                   NOT NULL,
          7   PRIMARY KEY (Guid)
          8) TYPE=InnoDB;

                根據該表寫了一個User類,并且加上了XDoclet的tag。XDoclet有關hibernate的tag可以參考:http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html

            1package sample.hibernate;
            2
            3import java.io.Serializable;
            4
            5/**
            6 * <p>Title: </p>
            7 *
            8 * <p>Description: </p>
            9 *
           10 * <p>Copyright: Copyright (c) 2005</p>
           11 *
           12 * <p>Company: </p>
           13 *
           14 * @author George Hill
           15 * @version 1.0
           16 */

           17
           18/**
           19 * @hibernate.class
           20 *  table="User"
           21 *  dynamic-update="true"
           22 *  dynamic-insert="true"
           23 */

           24public class User implements Serializable {
           25
           26  // identifier field
           27  private int guid;
           28
           29  // persistent field
           30  private String account;
           31
           32  // persistent field
           33  private String password;
           34
           35  // persistent field
           36  private String email;
           37
           38  /**
           39   * default constructor
           40   */

           41  public User() {}
           42
           43  /**
           44   * full constructor
           45   */

           46  public User(String account, String password, String email) {
           47    this.account = account;
           48    this.password = password;
           49    this.email = email;
           50  }

           51
           52  /**
           53   * @hibernate.id
           54   *  generator-class="native"
           55   *  type="int"
           56   *  column="Guid"
           57   */

           58  public int getGuid() {
           59    return guid;
           60  }

           61
           62  public void setGuid(int guid) {
           63    this.guid = guid;
           64  }

           65
           66  /**
           67   * @hibernate.property
           68   *  column="Account"
           69   *  length="64"
           70   *  not-null="true"
           71   */

           72  public String getAccount() {
           73    return account;
           74  }

           75
           76  public void setAccount(String account) {
           77    this.account = account;
           78  }

           79
           80  /**
           81   * @hibernate.property
           82   *  column="Password"
           83   *  length="16"
           84   *  not-null="true"
           85   */

           86  public String getPassword() {
           87    return password;
           88  }

           89
           90  public void setPassword(String password) {
           91    this.password = password;
           92  }

           93
           94  /**
           95   * @hibernate.property
           96   *  column="Email"
           97   *  length="128"
           98   *  not-null="true"
           99   */

          100  public String getEmail() {
          101    return email;
          102  }

          103
          104  public void setEmail(String email) {
          105    this.email = email;
          106  }

          107
          108}

          109

                為了生成hbm.xml文件,需要用到ant,下面是我的build.xml文件的內容:

           1<?xml version="1.0" encoding="ISO-8859-1"?>
           2
           3<project name="Hibernate XDoclet Examples" default="hibernate" basedir=".">
           4  <property name="xdoclet.root.dir" value="D:/lib/xdoclet-1.2.3"/>
           5  <property name="xdoclet.lib.dir" value="${xdoclet.root.dir}/lib"/>
           6
           7  <path id="sampleclasspath">
           8    <fileset dir="${xdoclet.lib.dir}">
           9      <include name="*.jar"/>
          10    </fileset>
          11  </path>
          12
          13  <taskdef
          14    name="hibernatedoclet"
          15    classname="xdoclet.modules.hibernate.HibernateDocletTask"
          16    classpathref="sampleclasspath"
          17    />
          18
          19  <target name="hibernate" description="Generate mapping documents">
          20
          21    <echo>+---------------------------------------------------+</echo>
          22    <echo>|                                                   |</echo>
          23    <echo>| R U N N I N G   H I B E R N A T E D O C L E T     |</echo>
          24    <echo>|                                                   |</echo>
          25    <echo>+---------------------------------------------------+</echo>
          26
          27    <hibernatedoclet
          28      destdir="./src"
          29      excludedtags="@version,@author,@todo,@see"
          30      addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
          31      force="false"
          32      verbose="true">
          33
          34      <fileset dir="./src">
          35        <include name="sample/hibernate/*.java"/>
          36      </fileset>
          37
          38      <hibernate version="2.1"/>
          39
          40    </hibernatedoclet>
          41  </target>
          42</project>
          43

                build.xml文件中的第四行是指定XDoclet的根路徑,需要根據解壓的路徑做相應的修改。XDoclet的下載地址:http://xdoclet.sourceforge.net/xdoclet/install.html。在taskdef里面需要指定classname為xdoclet.modules.hibernate.HibernateDocletTask。在34行的fileset里面需要指定你的Java類所在的包路徑。
                運行ant,可以看到生成的User.hbm.xml文件的內容如下:

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE hibernate-mapping PUBLIC
              "-//Hibernate/Hibernate Mapping DTD 2.0//EN" 
              "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
          >

          <hibernate-mapping
          >
              
          <class
                  
          name="sample.hibernate.User"
                  table
          ="User"
                  dynamic-update
          ="true"
                  dynamic-insert
          ="true"
              
          >

                  
          <id
                      
          name="guid"
                      column
          ="Guid"
                      type
          ="int"
                  
          >
                      
          <generator class="native">
                        
          <!--  
                            To add non XDoclet generator parameters, create a file named 
                            hibernate-generator-params-User.xml 
                            containing the additional parameters and place it in your merge dir. 
                        
          --> 
                      
          </generator>
                  
          </id>

                  
          <property
                      
          name="account"
                      type
          ="java.lang.String"
                      update
          ="true"
                      insert
          ="true"
                      column
          ="Account"
                      length
          ="64"
                      not-null
          ="true"
                  
          />

                  
          <property
                      
          name="password"
                      type
          ="java.lang.String"
                      update
          ="true"
                      insert
          ="true"
                      column
          ="Password"
                      length
          ="16"
                      not-null
          ="true"
                  
          />

                  
          <property
                      
          name="email"
                      type
          ="java.lang.String"
                      update
          ="true"
                      insert
          ="true"
                      column
          ="Email"
                      length
          ="128"
                      not-null
          ="true"
                  
          />

                  
          <!--
                      To add non XDoclet property mappings, create a file named
                          hibernate-properties-User.xml
                      containing the additional properties and place it in your merge dir.
                  
          -->

              
          </class>

          </hibernate-mapping>
          posted on 2005-05-17 10:20 小米 閱讀(2661) 評論(1)  編輯  收藏 所屬分類: Hibernate

          評論:
          # re: 如何用XDoclet生成hbm.xml文件 2008-11-14 13:14 | ww
          請教:為什么我的類映射文件不會自動生成?錯誤信息如下:
          generate-hbm:
          [echo] 運行HibernateDoclet,生成 Hibernate 類的映射文件
          [hibernatedoclet] (XDocletMain.start 47 ) Running <hibernate/>
          [hibernatedoclet] (XDocletMain.start 47 ) Running <hibernatecfg/>
          [hibernatedoclet] Generating hibernate.cfg.xml configuration file
          schemaexport:
          [echo] 運行SchemaExport,利用 hbm.xml 文件生成數據表
          [schemaexport] (cfg.Environment 500 ) Hibernate 3.2.0
          [schemaexport] (cfg.Environment 533 ) hibernate.properties not found
          [schemaexport] (cfg.Environment 667 ) Bytecode provider name : cglib
          [schemaexport] (cfg.Environment 584 ) using JDK 1.4 java.sql.Timestamp handling
          [schemaexport] (cfg.Configuration 1384) configuring from file: hibernate.cfg.xml
          [schemaexport] (util.DTDEntityResolver 46 ) Don't use old DTDs, read the Hibernate 3.x Migration Guide!
          [schemaexport] (util.XMLHelper 61 ) Error parsing XML: D:\testy\hibernate03\src\hibernate.cfg.xml(21) The content of element type "session-factory" is incomplete, it must match "(property*,mapping+,(class-cache|collection-cache|jcs-class-cache|jcs-collection-cache)*)".

          BUILD FAILED
          D:\testy\hibernate03\build.xml:101: Schema text failed: invalid configuration

          Total time: 3 seconds  回復  更多評論
            
          主站蜘蛛池模板: 大荔县| 阿拉善右旗| 莱西市| 锡林郭勒盟| 宁南县| 泾川县| 汝阳县| 永定县| 临澧县| 永川市| 新疆| 固安县| 廉江市| 抚松县| 罗源县| 绥江县| 丰宁| 罗定市| 隆尧县| 永和县| 阿拉善盟| 玉山县| 调兵山市| 上犹县| 古交市| 陇南市| 镇平县| 珠海市| 长兴县| 民勤县| 通江县| 海宁市| 泾源县| 武义县| 宁南县| 额尔古纳市| 壤塘县| 塘沽区| 邵武市| 三亚市| 昌都县|