西部村里人

          常用鏈接

          統計

          其它BLOG

          最新評論

          Hibernate XDoclet 在Eclipse中的模版

          開發Hibernate很久,最終還是XDoclet提供的幫助最大。能夠處理復雜的對象關系,也最符合面向對象的原則。
          方法1:
          OO類圖--〉數據庫設計--〉MiddleGen (能夠處理基本的關聯關系--打開XDoclet標簽生成開關,但不能處理繼承概念,較為遺憾ing)-->在Eclipse手工更新Java類中 的XDoclet標簽,然后XDoclet生成Hbm文件。當然了再寫個JUnit測試一下關聯關系是否正確,必要的Lazy是否標注。

          下載Template: http://raibledesigns.com/wiki/Wiki.jsp?page=XDocletEclipse#hibcolidx 非常感謝MattRaible.

          在中文環境中,window xp的字符切換鍵與Eclipse模版的字符快捷鍵重合,需要修改之。為了避免麻煩,直接修改Template文件,簡單添加@標示符:全文如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <templates>
              <template name="@hibarray" description="@hibernate.array" context="javadoc" enabled="true">@hibernate.array table=&quot;&quot; cascade=&quot;save-update&quot;</template>
              <template name="@hibbag" description="@hibernate.bag" context="javadoc" enabled="true">@hibernate.bag table=&quot;&quot; lazy=&quot;false&quot; cascade=&quot;none&quot; inverse=&quot;false&quot;</template>
              <template name="@hibclass" description="@hibernate.class" context="javadoc" enabled="true">@hibernate.class table=&quot;${enclosing_type}&quot;</template>
              <template name="@hibcolelm" description="@hibernate.collection-element" context="javadoc" enabled="true">@hibernate.collection-element column=&quot;&quot; type=&quot;&quot; length=&quot;&quot;</template>
              <template name="@hibcolidx" description="@hibernate.collection-index" context="javadoc" enabled="true">@hibernate.collection-index column=&quot;&quot; type=&quot;&quot; length=&quot;&quot;</template>
              <template name="@hibcolkey" description="@hibernate.collection-key" context="javadoc" enabled="true">@hibernate.collection-key column=&quot;&quot; generator-class=&quot;native&quot;</template>
              <template name="@hibcolmtm" description="@hibernate.many-to-many" context="javadoc" enabled="true">@hibernate.set name=&quot;${enclosing_method}&quot; table=&quot;link_table_name_here&quot; cascade=&quot;save-update&quot; inverse=&quot;true|false&quot; lazy=&quot;true&quot;
               * @hibernate.collection-key column=&quot;${enclosing_type}_ID&quot;
               * @hibernate.collection-many-to-many class=&quot;relationship_class_the_set_contains&quot; column=&quot;relationship_foreign_key&quot;
               * @return ${return_type}</template>
              <template name="@hibcolotm" description="@hibernate.one-to-many relationship" context="javadoc" enabled="true">@hibernate.set name=&quot;${enclosing_method}&quot; table=&quot;relationship_table&quot;
               *                     sort=&quot;comparator_class&quot; inverse=&quot;true|false&quot;
               *                     cascade=&quot;save-update&quot; lazy=&quot;true&quot;
               * @hibernate.collection-key column=&quot;${enclosing_type}_ID&quot;
               * @hibernate.collection-one-to-many class=&quot;relationship_class&quot;
               *
               * @return ${return_type}</template>
              <template name="@hibcomelm" description="@hibernate.collection-composite-element" context="javadoc" enabled="true">@hibernate.collection-composite-element class=&quot;&quot;</template>
              <template name="@hibcomp" description="@hibernate.component" context="javadoc" enabled="true">@hibernate.component class=&quot;component_class_name&quot;</template>
              <template name="@hibdisc" description="@hibernate.discriminator" context="javadoc" enabled="true">@hibernate.discriminator column=&quot;subclass&quot; type=&quot;character&quot;</template>
              <template name="@hibid" description="@hibernate.id" context="javadoc" enabled="true">Note: unsaved-value An identifier property value that indicates that an instance
               * is newly instantiated (unsaved), distinguishing it from transient instances that
               * were saved or loaded in a previous session.  If not specified you will get an exception like this:
               * another object associated with the session has the same identifier
               *
               * @hibernate.id generator-class=&quot;&quot; type=&quot;${return_type}&quot; column=&quot;${enclosing_type}_ID&quot;
               * unsaved-value=&quot;null&quot; length=&quot;&quot;
               * @return ${return_type}</template>
              <template name="@hiblist" description="@hibernate.list" context="javadoc" enabled="true">@hibernate.list table=&quot;relationship-table&quot; lazy=&quot;false&quot; cascade=&quot;none&quot;</template>
              <template name="@hibmap" description="@hibernate.map" context="javadoc" enabled="true">@hibernate.map name=&quot;${enclosing_method}&quot; table=&quot;relationship-table&quot; lazy=&quot;false&quot; cascade=&quot;none&quot;</template>
              <template name="@hibmto" description="@hibernate.many-to-one" context="javadoc" enabled="true">@hibernate.many-to-one column=&quot;${return_type}_ID&quot; class=&quot;package.${return_type}&quot;
               *
               * @return ${return_type}
               *</template>
              <template name="@hiboto" description="@hibernate.one-to-one" context="javadoc" enabled="true">hibernate.one-to-one cascade=&quot;none&quot; class=&quot;&quot; outer-join=&quot;auto&quot;</template>
              <template name="@hibprimarr" description="@hibernate.primitive-array" context="javadoc" enabled="true">@hibernate.primitive-array table=&quot;&quot; cascade=&quot;none&quot;</template>
              <template name="@hibprop" description="@hibernate.property" context="javadoc" enabled="true">@hibernate.property name=&quot;${enclosing_method}&quot; column=&quot;${enclosing_method}&quot; type=&quot;${return_type}&quot; not-null=&quot;false&quot; unique=&quot;false&quot;
               *
               * @return ${return_type}</template>
              <template name="@hibquery" description="@hibernate.query" context="javadoc" enabled="true">@hibernate.query name=&quot;&quot; query=&quot;&quot;</template>
              <template name="@hibset" description="@hibernate.set" context="javadoc" enabled="true">@hibernate.set name=&quot;${enclosing_method}&quot; table=&quot;relationship_table&quot;
               *                     sort=&quot;comparator_class&quot; inverse=&quot;true&quot;
               *                     cascade=&quot;save-update&quot; lazy=&quot;true&quot;</template>
              <template name="@hibsubc" description="@hibernate.subclass" context="javadoc" enabled="true">@hibernate.subclass name=&quot;&quot; discriminator-value=&quot;&quot;</template>
              <template name="@hibts" description="@hibernate.timestamp" context="javadoc" enabled="true">@hibernate.timestamp column=&quot;${enclosing_method}&quot;
               *
               * @return ${return_type}</template>
              <template name="@hibver" description="@hibernate.version" context="javadoc" enabled="true">@hibernate.version column=&quot;${enclosing_method}&quot;
               *
               * @return ${return_type}</template>
          </templates>

          使用時:先把XML內容單獨保存為文件,然后在Eclipse-->Windows-->Preferences
                                               在Preferences-->Java-->Editor-->Templates 點擊Import按鈕導入之前已經保存的XML文件。

          方法2:
          OO類圖--〉在Eclipse手工編寫屬性--〉生成Get/Set方法--〉更新Java類中 的XDoclet標簽,然后XDoclet生成Hbm文件。當然了再寫個JUnit測試一下關聯關系是否正確,必要的Lazy是否標注。
          要求先修改Get方法的模板:源代碼編輯器中鼠標右鍵--〉Source--〉Generate Getters And Setters..
          bb.PNG
                                
                         點擊打開面板中Code Template鏈接。
          aa1.PNG

          編輯Getter方法模板:
          /**
           * @hibernate.property name="${bare_field_name}" column="${field}" type="${field_type}" not-null="false" unique="false" length="128"
           * @return Returns the ${bare_field_name}.
           */
          然后生成代碼,手工微調部分屬性。也能夠節約大量時間。

          注意:在編寫Java POJO類時,java屬性用完整的帶包名的類,例如:
          /**
          * @author jdyao
           * @hibernate.class table="respri"
           * @version
           */
          public class Resource implements Serializable {

              private static final long serialVersionUID = 1505581058179605003L;

              private java.lang.String guid;

              private java.lang.String context;

           

              public
          Resource () {

              }

              /**
               * @return java.lang.String
               * @hibernate.property name="context" type="java.lang.String"
               *                     length="128"
               *
               */
              public java.lang.String getContext() {
                  return context;
              }

              public void setContext(java.lang.String context) {
                  this.context = context;
              }

              /**
               * @return java.lang.String
               * @hibernate.id generator-class="guid" type="java.lang.String" column="guid"
               *               unsaved-value="null" length="38"
               */
              public java.lang.String getGuid() {
                  return guid;
              }

              public void setGuid(java.lang.String guid) {
                  this.guid = guid;
              }

          }
          原因:XDoclet在生成的時候,如果type="string",有時會出現錯誤,無法生成Hbm文件,為了避免這個不必要的錯誤,務必要寫全類名。

          XDoclet build.xml文件:

          <?xml version="1.0" encoding="ISO-8859-1"?>

          <project name="XDoclet Examples" default="hibernate" basedir=".">
              <property name="xdoclet.root.dir" value="${basedir}"/>
              <property file="${xdoclet.root.dir}/build.properties"/>

              <!-- Include the build-dist properties. Since properties are immutable,
              this will not override available properties. You do not have to include
              this in your own build file. -->
              <property file="build-dist.properties"/>

              <!-- See CustomerBean. This is to demonstrate property substitution. -->
              <property name="ejb.prefix" value="blah"/>

              <!-- =================================================================== -->
              <!-- Define the class path                                               -->
              <!-- =================================================================== -->
              <path id="samples.class.path">
                  <fileset dir="${lib.dir}">
                      <include name="*.jar"/>
                  </fileset>
                  <fileset dir="${samples.lib.dir}">
                      <include name="*.jar"/>
                  </fileset>
                  <fileset dir="${dist.lib.dir}">
                      <include name="*.jar"/>
                  </fileset>
              </path>

              <!-- =================================================================== -->
              <!-- Initialise                                                          -->
              <!-- =================================================================== -->
              <target name="init">
                  <tstamp>
                      <format property="TODAY" pattern="d-MM-yy"/>
                  </tstamp>
                  <taskdef
                      name="xdoclet"
                      classname="xdoclet.DocletTask"
                      classpathref="samples.class.path"
                      />
                   <taskdef
                      name="hibernatedoclet"
                      classname="xdoclet.modules.hibernate.HibernateDocletTask"
                      classpathref="samples.class.path"
                      />
              </target>


              <!-- =================================================================== -->
              <!-- Prepares the directory structure                                    -->
              <!-- =================================================================== -->
              <target name="prepare" depends="init">
                  <mkdir dir="${samples.classes.dir}"/>
                  <mkdir dir="${samples.gen-src.dir}"/>
                  <mkdir dir="${samples.meta-inf.dir}"/>
              </target>



              <!-- =================================================================== -->
              <!-- Invoke XDoclet's hibernate                                          -->
              <!-- =================================================================== -->
              <target name="hibernate" depends="prepare" description="Generate mapping documents (run jar first)">

                  <echo>+---------------------------------------------------+</echo>
                  <echo>|                                                   |</echo>
                  <echo>| R U N N I N G   H I B E R N A T E D O C L E T     |</echo>
                  <echo>|                                                   |</echo>
                  <echo>+---------------------------------------------------+</echo>

                  <hibernatedoclet
                      destdir="${samples.gen-src.dir}"
                      mergedir="${samples.src.dir}"
                      excludedtags="@version,@author,@todo,@see"
                      addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
                      force="${samples.xdoclet.force}"
                      verbose="false">

                      <fileset dir="${samples.java.dir}">
                          <include name="**/**/*.java"/>
                      </fileset>

                      <hibernate version="3.0"/>

                  </hibernatedoclet>
              </target>

              <!-- =================================================================== -->
              <!-- Clean                                                               -->
              <!-- =================================================================== -->
              <target name="clean">
                  <delete dir="${samples.dist.dir}"/>
              </target>

          </project>
          build-dist.properties 文件:
          # These properties are only used when building the samples expanded from the distribution.

          lib.dir = ${xdoclet.root.dir}/lib
          dist.lib.dir = ${lib.dir}

          samples.dir = ${xdoclet.root.dir}
          samples.dist.dir = ${samples.dir}/target
          samples.lib.dir = ${samples.dir}/lib
          samples.src.dir = ${samples.dir}/src
          samples.java.dir = ${samples.src.dir}/java
          samples.gen-src.dir = ${samples.dist.dir}/gen-src

          samples.meta-inf.dir = ${samples.dist.dir}/meta-inf
          samples.web-inf.dir = ${samples.dist.dir}/web-inf
          samples.merge.dir = ${samples.src.dir}/merge
          samples.classes.dir = ${samples.dist.dir}/classes
          samples.web.dir = ${samples.src.dir}/web
          samples.xdoclet.force = false

          工程目錄結構:從XDoclet網站下載該包,解壓縮后,把Example目錄單獨copy出來,把這2個文件放在Example目錄下,同時建立lib目錄,把XDoclet目錄下--〉lib目錄下的*.jar拷貝到Example新建立的lib目錄下。

          posted on 2006-03-10 00:28 西部村里人 閱讀(1451) 評論(0)  編輯  收藏 所屬分類: Hibernate


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 汝州市| 建水县| 平和县| 抚远县| 全南县| 高淳县| 南京市| 青河县| 静安区| 绥阳县| 乌恰县| 黎川县| 工布江达县| 三明市| 蒙阴县| 桃源县| 塔河县| 色达县| 从化市| 仁寿县| 礼泉县| 上思县| 辉县市| 东阳市| 额尔古纳市| 铁岭县| 宁晋县| 钦州市| 松江区| 红原县| 本溪市| 东方市| 泊头市| 巩义市| 教育| 青州市| 南充市| 福海县| 三河市| 友谊县| 电白县|