<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 閱讀(2551) 評論(0)  編輯  收藏 所屬分類: javahibernate

    主站蜘蛛池模板: 亚洲1234区乱码| 国产亚洲婷婷香蕉久久精品| 亚洲日本VA午夜在线电影| 最近中文字幕无吗高清免费视频| 亚洲乱码中文字幕在线| 亚洲精品一级无码中文字幕| 免费91麻豆精品国产自产在线观看 | 亚洲а∨天堂久久精品| 中文字幕av无码不卡免费| 亚洲精品日韩中文字幕久久久| 国内一级一级毛片a免费| 添bbb免费观看高清视频| 亚洲人成网77777亚洲色| 91精品成人免费国产片| 国产精品亚洲а∨无码播放不卡| 国产AV无码专区亚洲AVJULIA| 99re在线免费视频| 在线视频亚洲一区| 日韩免费a级毛片无码a∨| 亚洲国产综合AV在线观看| 亚洲国产精品无码专区影院| 日韩免费视频网站| 免费无码一区二区三区蜜桃 | 亚洲熟妇色自偷自拍另类| 四虎永久在线精品视频免费观看| 中国一级毛片免费看视频| 亚洲乱妇老熟女爽到高潮的片| 国产亚洲精品久久久久秋霞 | 亚洲国产综合人成综合网站| 久久ww精品w免费人成| 免费一区二区无码视频在线播放| 亚洲色图古典武侠| 亚洲一级特黄大片在线观看| 妞干网在线免费视频| 无码免费一区二区三区免费播放| 亚洲AV永久无码精品网站在线观看| 亚洲伦另类中文字幕| 亚洲无码精品浪潮| 永久免费无码网站在线观看| 免费在线看v网址| 午夜无码A级毛片免费视频|