躺在沙灘上的小豬

          快樂的每一天

          XDoclet 2 for Hibernate 3

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

          1:一個User對應(yīng)多篇BlogEntity
          2:一篇BlogEntity可以對應(yīng)多個Tag,對應(yīng)多個Category,對應(yīng)一個User
          3:一個Category對應(yīng)多個BlogEntity
          4:一個Tag對應(yīng)多個BlogEntity

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

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

          主站蜘蛛池模板: 临武县| 临猗县| 宁乡县| 广昌县| 手机| 基隆市| 民丰县| 鄂温| 邯郸县| 玉溪市| 临夏市| 门源| 宣汉县| 河南省| 信丰县| 芜湖县| 泽普县| 内乡县| 容城县| 虞城县| 遵化市| 宜阳县| 苗栗县| 定远县| 贵州省| 宁蒗| 信丰县| 定陶县| 淮滨县| 祥云县| 平遥县| 永年县| 溧阳市| 临沂市| 福清市| 鄱阳县| 五河县| SHOW| 新巴尔虎右旗| 江北区| 诏安县|