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

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

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

    躺在沙灘上的小豬

    快樂的每一天

    XDoclet 2 for Hibernate 3

    看代碼說話:)
    我們舉個簡單的例子,一個blog有用戶User,文章BlogEntity,文章分類Category以及現在很流行的Tag.(為了簡單,這里例子舉的是單用戶,及不需要考慮Category,Tag與User的對應關系)

    1:一個User對應多篇BlogEntity
    2:一篇BlogEntity可以對應多個Tag,對應多個Category,對應一個User
    3:一個Category對應多個BlogEntity
    4:一個Tag對應多個BlogEntity

    OK,這四個對象簡單的關系為:
    1:User和BlogEntity是一對多的關系
    2:BlogEntity和Category是多對多的關系
    3:BlogEntity和Tag是多對多的關系

    User.java

    package martin.xpost.model;

    import martin.xpost.hibernate.UserDAO;
    import martin.xpost.util.ListUtil;

    import java.util.Set;

    /**
     * 
    @author martin
     * 
    @version 1.0
     * @hibernate.class table="t_user"
     
    */

    public class User extends PersistentImpl {
        
    /**
         * @hibernate.property not-null="true"
         
    */

        
    private String userName;

        
    /**
         * @hibernate.property not-null="true"
         
    */

        
    private String password;

        
    /**
         * @hibernate.property
         
    */

        
    private String realName;

        
    /**
         * @hibernate.property
         
    */

        
    private String email;

        
    /**
         * @hibernate.set table="t_blogentity" lazy="true" inverse="true" cascade="all-delete-orphan"
         * @hibernate.key column="userid"
         * @hibernate.one-to-many class="martin.xpost.model.BlogEntity"
         
    */

        
    private Set blogEntities;

        
    //----------------------------------------------------------------
        /// getter and setter
        
    //----------------------------------------------------------------
    }


    BlogEntity.java

    package martin.xpost.model;

    import java.util.Set;

    /**
     * 
    @author martin
     * 
    @version 1.0
     * @hibernate.class table="t_blogentity"
     
    */

    public class BlogEntity extends PersistentImpl {
        
    /**
         * @hibernate.property not-null="true"
         
    */

        
    private String title;

        
    /**
         * @hibernate.property
         
    */

        
    private String shortBrief;

        
    /**
         * @hibernate.property
         
    */

        
    private String content;


        
    /**
         * @hibernate.many-to-one column="userid" not-null="true"
         
    */

        
    private User user;

        
    /**
         * @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
         * @hibernate.key column="blogentityid"
         * @hibernate.many-to-many class="martin.xpost.model.Category" column="categoryid"
         
    */

        
    private Set categories;

        
    /**
         * @hibernate.set table="t_blogentity_tag" lazy="true" cascade="none"
         * @hibernate.key column="blogentityid"
         * @hibernate.many-to-many class="martin.xpost.model.Tag" column="tagid"
         
    */

        
    private Set tags;

        
    //----------------------------------------------------------------
        /// getter and setter
        
    //----------------------------------------------------------------
    }

    Category.java

    package martin.xpost.model;

    import java.util.Set;

    /**
     * 
    @author martin
     * @hibernate.class table="t_category"
     
    */

    public class Category extends PersistentImpl {
        
    /**
         * @hibernate.property not-null="true"
         
    */

        
    private String categoryName;

        
    /**
         * @hibernate.property
         
    */

        
    private String description;

        
    /**
         * @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
         * @hibernate.key column="categoryid"
         * @hibernate.many-to-many class="martin.model.xpost.BlogEntity" column="blogentityid"
         
    */

        
    private Set blogEntities;

        
    //----------------------------------------------------------------
        /// getter and setter
        
    //----------------------------------------------------------------
    }

    Tag.java
    package martin.xpost.model;

    import java.util.Set;

    /**
     * 
    @author martin
     * 
    @version 1.0
     * @hibernate.class table="t_tag"
     
    */

    public class Tag extends PersistentImpl {
        
    /**
         * @hibernate.property
         
    */

        
    private String tagName;

        
    /**
         * @hibernate.set table="t_blogentity_tag"  lazy="true" cascade="none"
         * @hibernate.key column="tagid"
         * @hibernate.many-to-many class="martin.xpost.model.BlogEntity" column="blogentityid"
         
    */

        
    private Set blogEntities;

        
    //----------------------------------------------------------------
        /// getter and setter
        
    //----------------------------------------------------------------
    }

    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    好了,這個沒什么好說的,看代碼就知道怎么用了,下面我們通過ant生成hbm.xml文件.
    一:下載xdoclet 2: http://xdoclet.codehaus.org

    二:編寫ant腳本
    <?xml version="1.0"?>
    <project name="xpost" default="init">
        
    <property name="src.java.dir" value="src"/>

        
    <property name="build.dir" value="WEB-INF/classes"/>
        
    <property name="hbm.dir" value="WEB-INF/classes"/>

        
    <property name="xdoclet.lib.dir" value="build/lib/xdoclet"/>
        
    <property name="build.lib.dir" value="build/lib/runtime"/>

        
    <path id="xdoclet.class.path">
            
    <fileset dir="${xdoclet.lib.dir}">
                
    <include name="**/*.jar"/>
            
    </fileset>
        
    </path>

        
    <path id="build.class.path">
            
    <fileset dir="${build.lib.dir}">
                
    <include name="**/*.jar"/>
            
    </fileset>
        
    </path>

        
    <target name="init">
            
    <mkdir dir="${build.dir}"/>
        
    </target>

        
    <target name="compile">
            
    <javac srcdir="${src.java.dir}"
                   destdir
    ="${build.dir}"
                   classpathref
    ="build.class.path"/>
        
    </target>

        
    <target name="removehbm">
            
    <delete dir="${src.java.dir}" includes="**/model/*.xml"/>
        
    </target>

        
    <target name="hibernatedoclet"
                depends
    ="removehbm"
                description
    ="Generate Persistence and form classes">

            
    <taskdef
                    
    name="xdoclet"
                    classname
    ="org.xdoclet.ant.XDocletTask"
                    classpathref
    ="xdoclet.class.path"/>
            
    <xdoclet>
                
    <!-- defines the file handled by xdoclet2 -->
                
    <fileset dir="${src.java.dir}">
                    
    <include name="**/model/*.java"/>
                
    </fileset>
                
    <!-- defines the processing of a plugin -->
                
    <component
                        
    classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
                        destdir
    ="${src.java.dir}"
                        version
    ="3.0"/>
            
    </xdoclet>
        
    </target>

        
    <target
                
    name="jarhbm"
                depends
    ="hibernatedoclet">
            
    <jar
                    
    basedir="${src.java.dir}"
                    includes
    ="**/model/*.xml"
                    destfile
    ="${hbm.dir}/xpost.hbm.jar"/>
        
    </target>

    </project>

    ----------------------------------------------------------------------------
    ----------------------------------------------------------------------------
    SchemaExport Test

    JUnitHelper.java

    package martin.xpost.util;

    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    import java.io.File;

    /**
     * 
    @author martin
     
    */

    public class JUnitHelper {
        
    public static ApplicationContext getApplicationContext() {
            
    return new FileSystemXmlApplicationContext("D:\\workspace\\projects\\xpost\\WEB-INF\\applicationContext.xml");
        }


        
    public static void initEnviroment() {
            Configuration config 
    = new Configuration()
                    .addJar(
    new File("D:\\workspace\\projects\\xpost\\WEB-INF\\classes\\xpost.hbm.jar"));
            
    new SchemaExport(config).create(truetrue);
        }

    }

    SchemaExportTest.java
    package martin.xpost;

    import junit.framework.TestCase;
    import martin.xpost.util.JUnitHelper;

    /**
     * 
    @author martin
     
    */

    public class SchemaExportTest extends TestCase {
        
    public void testExport() {
            JUnitHelper.initEnviroment();
        }

    }

    參考:
    1:http://www.hibernate.org/338.html
    2:http://xdoclet.codehaus.org

    posted on 2006-01-25 14:10 martin xus 閱讀(2548) 評論(0)  編輯  收藏 所屬分類: javahibernate

    主站蜘蛛池模板: 亚洲乱码av中文一区二区| 性xxxx黑人与亚洲| 国产免费人成视频在线观看| 久久午夜夜伦鲁鲁片无码免费| 麻豆亚洲AV永久无码精品久久| 国产成人精品免费视频大全麻豆| 亚洲国产精品成人精品小说| 久久精品国产精品亚洲精品| avtt亚洲天堂| 国产精品免费看久久久久| 黄页网站免费观看| 日韩插啊免费视频在线观看| 亚洲一区二区三区丝袜| 亚洲综合久久1区2区3区| 亚洲av永久无码精品秋霞电影影院| 久久这里只有精品国产免费10| 一级毛片正片免费视频手机看| 亚洲男人第一av网站| 国产亚洲情侣一区二区无码AV| 91黑丝国产线观看免费| 51在线视频免费观看视频| 欧美色欧美亚洲另类二区| 亚洲综合国产成人丁香五月激情| 亚洲性猛交XXXX| 国产性爱在线观看亚洲黄色一级片 | 亚洲视频一区二区三区四区| 亚洲男人的天堂在线播放| 亚洲AV乱码久久精品蜜桃| 亚洲成亚洲乱码一二三四区软件| 天天摸夜夜摸成人免费视频| 黄色片在线免费观看| 最近中文字幕大全免费视频| 最近中文字幕免费mv在线视频| 免费无码专区毛片高潮喷水| 黄色免费网站在线看| 视频免费1区二区三区| 亚洲精品第一国产综合野| 亚洲三级高清免费| 色婷婷亚洲十月十月色天| 亚洲精品在线免费观看| 亚洲一区中文字幕在线电影网|