<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    CONAN ZONE

    你越掙扎我就越興奮

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      0 Posts :: 282 Stories :: 0 Comments :: 0 Trackbacks
    在NetBeans5.0中使用xdoclet生成hibernate的*.hbm.xml文件

    敬告:
    ------------------------------------------------------------
        本文作者希望本文可以在網(wǎng)絡(luò)及任何媒體進(jìn)行任何形式的傳播、復(fù)
    制、引用、修改,在此過程中請保留此敬告內(nèi)容。

                                     謝謝!
                                                        popo
    ------------------------------------------------------------

    NetBeans是基于ANT進(jìn)行項(xiàng)目的build和run的,所以我們可以方便的加入自己的target對原有項(xiàng)目進(jìn)行擴(kuò)展。
    啟動(dòng)NetBeans,打開以前寫的SimpleWebSite_dao項(xiàng)目,因?yàn)槲覀円龅氖抢肵Doclet生成Hibernate的*.hbm.xml(ORM)文件,所以將XDoclet的所有Jar包解壓到lib\xdoclet-1.3-SNAPSHOT目錄下,不用在項(xiàng)目中引用XDoclet的包,因?yàn)槲覀兪且ㄟ^修改Build文件來引用這些包。
    選擇“Files”窗口,展開SimpleWebSite_dao項(xiàng)目。
    打開build.xml文件,看該文件中的注釋,大概就是講NetBeans為我們生成的build文件是nbproject/build-impl.xml,build.xml已經(jīng)import了build-impl文件,如果我們想加入自己的target或者對原有target進(jìn)行修改,只要改動(dòng)build文件就可以。
    用NetBeans編輯XML文件很方便,可以對XML文件進(jìn)行檢查和校驗(yàn),并且還有代碼提示及補(bǔ)齊的功能:)。導(dǎo)航窗口更是為我們查看XML內(nèi)容結(jié)構(gòu)提供了極大的方便。
    在build.xml中加入以下內(nèi)容:

        <property name="lib.dir" location="../lib" />
        <property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />

        <path id="xdoclet.classpath">
            <fileset dir="${lib.dir}" includes="*.jar"/>
            <fileset dir="${xdoclet.dir}" includes="*.jar"/>
        </path>
        <!-- =================================================================== -->
        <!-- The "hibernatedoclet" target generates Hibernate mapping files      -->
        <!-- based on XDoclet marked-up Plain Old Java Object (POJO)             -->
        <!-- =================================================================== -->
        <target name="hibernatedoclet" depends="compile" unless="hibernatedoclet.unnecessary"
            description="Generate Hibernate mapping files">

            <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.classpath"/>
           
            <!-- generate hibernate files -->
            <hibernatedoclet destdir="${build.classes.dir}"  mergedir="${build.classes.dir}"
                excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
                force="${xdoclet.force}">
                <fileset dir="src"/>
                <hibernate validatexml="true" version="3.0"/>
            </hibernatedoclet>
        </target>

    用右鍵點(diǎn)build.xml文件,在“Run target”中,會看到我們新加的target->“hibernatedoclet”。
    現(xiàn)在寫一個(gè)POJO,用這個(gè)target生成.hbm.xml文件。
    New 一個(gè)Java Class  Customer(這個(gè)類的代碼是從網(wǎng)上拷貝來的,因?yàn)槲覍Doclet還不太熟悉):
    package hibernate;
    import java.util.Set;
    import java.util.Collections;
    /**
     * @author roson
     * @since 1.0
     * @version 1.0
     * @hibernate.class tables="customers"
     */
    public class Customer {
        /**This customer's identifier field.
         */
        private long id;
       
        /**This customer's name field.
         */
        private String name;
       
        /**The customer's orders set.
         */
        private Set orders=Collections.EMPTY_SET;
       
        /**The default construtor for Hibernate to instantiate with.
         */
        public Customer() {}
       
        /**The getter method for this Customer's identifier.
         *
         * @hibernate.id generator-class="native"
         */
        public long getId() {
            return id;
        }
       
        /**The setter method for this Customer's identifier.
         */
        public void setId(long id) {
            this.id=id;
        }
       
        /**The getter method for this Customer's name.
         *
         * @hibernate.property
         */
        public String getName() {
            return name;
        }
       
        /**The setter method for this Customer's name.
         */
        public void setName(String name) {
            this.name=name;
        }
       
        /**The getter method for this Customer's orders.
         *
         * @hibernate.set role="orders"
         *
         * @hibernate.collection-key column="customer_id"
         *
         * @hibernate.collection-one-to-many class="Order"
         */
        public Set getOrders() {
            return orders;
        }
       
        /**The setter method for this Customer's orders.
         */
        public void setOrders(Set orders) {
            this.orders=orders;
        }
    }

    現(xiàn)在右鍵點(diǎn)build.xml文件,在“Run target”中,選“hibernatedoclet”,BUILD SUCCESSFUL。
    在build\classes\hibernate目錄下會看到編譯后的Customer.class文件和XDoclet生成的Customer.hbm.xml。

    為了方便,我們進(jìn)一步修改build.xml文件,將build-impl.xml中的“jar”這個(gè)target復(fù)制粘貼過來,并改為以下內(nèi)容:
        <target name="jar" depends="init,hibernatedoclet,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
    我只把depends中的compile改為了hibernatedoclet,這樣在編譯后和打包前的時(shí)候,會生成*.hbm.xml文件。

    現(xiàn)在的build.xml文件內(nèi)容如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- You may freely edit this file. See commented blocks below for -->
    <!-- some examples of how to customize the build. -->
    <!-- (If you delete it and reopen the project it will be recreated.) -->
    <project name="SimpleWebSite_Dao" default="default" basedir=".">
        <description>Builds, tests, and runs the project SimpleWebSite_Dao.</description>
        <import file="nbproject/build-impl.xml"/>
        <property name="lib.dir" location="../lib" />
        <property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />

        <path id="xdoclet.classpath">
            <fileset dir="${lib.dir}" includes="*.jar"/>
            <fileset dir="${xdoclet.dir}" includes="*.jar"/>
        </path>
        <!--

        There exist several targets which are by default empty and which can be
        used for execution of your tasks. These targets are usually executed
        before and after some main targets. They are:

        -pre-init:                 called before initialization of project properties
        -post-init:                called after initialization of project properties
        -pre-compile:              called before javac compilation
        -post-compile:             called after javac compilation
        -pre-compile-single:       called before javac compilation of single file
        -post-compile-single:      called after javac compilation of single file
        -pre-compile-test:         called before javac compilation of JUnit tests
        -post-compile-test:        called after javac compilation of JUnit tests
        -pre-compile-test-single:  called before javac compilation of single JUnit test
        -post-compile-test-single: called after javac compilation of single JUunit test
        -pre-jar:                  called before JAR building
        -post-jar:                 called after JAR building
        -post-clean:               called after cleaning build products

        (Targets beginning with '-' are not intended to be called on their own.)

        Example of inserting an obfuscator after compilation could look like this:

        <target name="-post-compile">
        <obfuscate>
        <fileset dir="${build.classes.dir}"/>
        </obfuscate>
        </target>

        For list of available properties check the imported
        nbproject/build-impl.xml file.


        Another way to customize the build is by overriding existing main targets.
        The targets of interest are:

        -init-macrodef-javac:     defines macro for javac compilation
        -init-macrodef-junit:     defines macro for junit execution
        -init-macrodef-debug:     defines macro for class debugging
        -init-macrodef-java:      defines macro for class execution
        -do-jar-with-manifest:    JAR building (if you are using a manifest)
        -do-jar-without-manifest: JAR building (if you are not using a manifest)
        run:                      execution of project
        -javadoc-build:           Javadoc generation
        test-report:              JUnit report generation

        An example of overriding the target for project execution could look like this:

        <target name="run" depends="SimpleWebSite_Dao-impl.jar">
        <exec dir="bin" executable="launcher.exe">
        <arg file="${dist.jar}"/>
        </exec>
        </target>

        Notice that the overridden target depends on the jar target and not only on
        the compile target as the regular run target does. Again, for a list of available
        properties which you can use, check the target you are overriding in the
        nbproject/build-impl.xml file.

        -->
        <!-- =================================================================== -->
        <!-- The "hibernatedoclet" target generates Hibernate mapping files      -->
        <!-- based on XDoclet marked-up Plain Old Java Object (POJO)             -->
        <!-- =================================================================== -->
        <target name="hibernatedoclet" depends="compile" unless="hibernatedoclet.unnecessary"
            description="Generate Hibernate mapping files">

            <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.classpath"/>
           
            <!-- generate hibernate files -->
            <hibernatedoclet destdir="${build.classes.dir}"  mergedir="${build.classes.dir}"
                excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
                force="${xdoclet.force}">
                <fileset dir="src"/>
                <hibernate validatexml="true" version="3.0"/>
            </hibernatedoclet>
        </target>
        <target name="jar" depends="init,hibernatedoclet,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
       
    </project>

    在Projects窗口,選“Build Project”或者“Clean and Build Project”,
    會看到用到了我們修改后的jar這個(gè)target進(jìn)行的編譯和打包。
    XDoclet的功能很強(qiáng)大,我會邊學(xué)邊在我的項(xiàng)目中利用它,可以省去很多編碼工作。

    看Appfuse的build.xml,數(shù)據(jù)庫的表是用Hibernate提供的SchemaExportTask工具生成數(shù)據(jù)庫建表的SQL,我們也來照做,這樣只要在寫POJO時(shí)加上注釋,用ANT的target就可以生成*.hbm.xml文件和創(chuàng)建數(shù)據(jù)表的SQL語句。方便吧?
    對build.xml進(jìn)行修改,把生成*.hbm.xml文件的目標(biāo)路徑改為SRC目錄,不再depends="compile",改為在jar的時(shí)候先“hibernatedoclet”再“compile”,增加create_tables_sql的target,修改后的build.xml文件為:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- You may freely edit this file. See commented blocks below for -->
    <!-- some examples of how to customize the build. -->
    <!-- (If you delete it and reopen the project it will be recreated.) -->
    <project name="SimpleWebSite_Dao" default="default" basedir=".">
        <description>Builds, tests, and runs the project SimpleWebSite_Dao.</description>
        <import file="nbproject/build-impl.xml"/>
        <property name="src.dir" location="./src" />
        <property name="lib.dir" location="../lib" />
        <property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />

        <path id="xdoclet.classpath">
            <fileset dir="${lib.dir}" includes="*.jar"/>
            <fileset dir="${xdoclet.dir}" includes="*.jar"/>
        </path>
        <!--

        There exist several targets which are by default empty and which can be
        used for execution of your tasks. These targets are usually executed
        before and after some main targets. They are:

        -pre-init:                 called before initialization of project properties
        -post-init:                called after initialization of project properties
        -pre-compile:              called before javac compilation
        -post-compile:             called after javac compilation
        -pre-compile-single:       called before javac compilation of single file
        -post-compile-single:      called after javac compilation of single file
        -pre-compile-test:         called before javac compilation of JUnit tests
        -post-compile-test:        called after javac compilation of JUnit tests
        -pre-compile-test-single:  called before javac compilation of single JUnit test
        -post-compile-test-single: called after javac compilation of single JUunit test
        -pre-jar:                  called before JAR building
        -post-jar:                 called after JAR building
        -post-clean:               called after cleaning build products

        (Targets beginning with '-' are not intended to be called on their own.)

        Example of inserting an obfuscator after compilation could look like this:

        <target name="-post-compile">
        <obfuscate>
        <fileset dir="${build.classes.dir}"/>
        </obfuscate>
        </target>

        For list of available properties check the imported
        nbproject/build-impl.xml file.


        Another way to customize the build is by overriding existing main targets.
        The targets of interest are:

        -init-macrodef-javac:     defines macro for javac compilation
        -init-macrodef-junit:     defines macro for junit execution
        -init-macrodef-debug:     defines macro for class debugging
        -init-macrodef-java:      defines macro for class execution
        -do-jar-with-manifest:    JAR building (if you are using a manifest)
        -do-jar-without-manifest: JAR building (if you are not using a manifest)
        run:                      execution of project
        -javadoc-build:           Javadoc generation
        test-report:              JUnit report generation

        An example of overriding the target for project execution could look like this:

        <target name="run" depends="SimpleWebSite_Dao-impl.jar">
        <exec dir="bin" executable="launcher.exe">
        <arg file="${dist.jar}"/>
        </exec>
        </target>

        Notice that the overridden target depends on the jar target and not only on
        the compile target as the regular run target does. Again, for a list of available
        properties which you can use, check the target you are overriding in the
        nbproject/build-impl.xml file.

        -->
        <!-- =================================================================== -->
        <!-- The "hibernatedoclet" target generates Hibernate mapping files      -->
        <!-- based on XDoclet marked-up Plain Old Java Object (POJO)             -->
        <!-- =================================================================== -->
        <target name="hibernatedoclet" unless="hibernatedoclet.unnecessary"
            description="Generate Hibernate mapping files">

            <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.classpath"/>
           
            <!-- generate hibernate files -->
            <hibernatedoclet destdir="${src.dir}"  mergedir="${src.dir}"
                excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
                force="${xdoclet.force}">
                <fileset dir="${src.dir}"/>
                <hibernate validatexml="true" version="3.0"/>
            </hibernatedoclet>
        </target>
       
        <!-- =================================================================== -->
        <!-- The "create_tables_sql" target generates the database schema and    -->
        <!-- creates tables based on the mapping files                           -->
        <!-- =================================================================== -->
        <target name="create_tables_sql" depends="hibernatedoclet" description="creates database tables">
            <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
            classpathref="xdoclet.classpath"/>
           
            <schemaexport quiet="yes" text="true" drop="no" delimiter=";"
                properties="src/database.properties" output="../database/sql/create-tables.sql">
                <fileset dir="${src.dir}" includes="**/*.hbm.xml"/>
            </schemaexport>
        </target>
        <target name="jar" depends="init,create_tables_sql,compile,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
       
    </project>

    這樣,每次“Build Project”或者“Clean and Build Project”時(shí),都會先在源程序目錄創(chuàng)建*.hbm.xml文件,再在../database/sql目錄下生成創(chuàng)建數(shù)據(jù)表的SQL文件,再進(jìn)行編譯,打包。

    用上面的示例文件有幾個(gè)問題
    1.Customer類中 * @hibernate.class tables="customers"應(yīng)該是 * @hibernate.class table="customers",
    2.生成的Customer.hbm.xml文件“create_tables_sql”時(shí)失敗,提示信息為:
    D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\build.xml:107: Schema text failed: Could not read mapping document from file: D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\src\hibernate\Customer.hbm.xml
    BUILD FAILED (total time: 4 seconds)
    為字段加上數(shù)據(jù)類型(type="java.lang.Long")后可以讀取文件,但還是失敗:
    D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\build.xml:107: Schema text failed: Association references unmapped class: Order
    因?yàn)閷Doclet不熟,對Hibernate的配置也快忘記差不多了,所以省點(diǎn)事兒,先把Order屬性去掉。

    “Clean and Build Project”,哈哈,看看,*.hbm.xml文件和建數(shù)據(jù)庫表的Sql語句都生成啦。
    不過還沒有試過Debug等target,如果那些target依賴生成的*.hbm.xml文件的話,還要對相應(yīng)的target加上depends...

    剩下的就是要多看看XDoclet和Hibernate配置的文檔啦。
    posted on 2008-06-24 21:02 CONAN 閱讀(451) 評論(0)  編輯  收藏 所屬分類: NetBeans
    主站蜘蛛池模板: 亚洲午夜福利精品无码| 亚洲免费一级视频| 亚洲一级毛片免费看| 亚洲日韩精品国产一区二区三区| 国产成人无码免费视频97| a视频在线观看免费| 亚洲日韩看片无码电影| 国产亚洲精品线观看动态图| 成人浮力影院免费看| 一级视频在线免费观看| 亚洲国产精品yw在线观看| 亚洲人妻av伦理| 成人a免费α片在线视频网站| 国产视频精品免费视频| 欧洲 亚洲 国产图片综合| 亚洲免费人成在线视频观看| 国产在线观看片a免费观看| 国产午夜精品理论片免费观看 | 欧洲 亚洲 国产图片综合| 怡红院亚洲怡红院首页| 亚洲人成网站免费播放| 少妇性饥渴无码A区免费| 亚洲精品无码久久久久A片苍井空| 亚洲AV午夜成人片| 免费一级毛片正在播放| 99久久国产热无码精品免费| A国产一区二区免费入口| 欧美亚洲精品一区二区| 亚洲成人免费网址| 亚洲AV无码一区东京热久久| 午夜国产大片免费观看| 特级做A爰片毛片免费69| 午夜影院免费观看| A片在线免费观看| 免费又黄又爽又猛大片午夜| 亚洲国产日韩在线成人蜜芽| 国产亚洲av片在线观看播放| heyzo亚洲精品日韩| 精品免费久久久久久成人影院| 国产成人精品免费视频动漫| 国产好大好硬好爽免费不卡 |