posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          hbn 繼承關系映射_1

          Posted on 2007-12-26 11:41 G_G 閱讀(1115) 評論(0)  編輯  收藏 所屬分類: hibernate
          嘗試著繼承關系和xdoclet使用后,對數(shù)據(jù)庫面向對象的使用更近一步。
          這次例子從開始到結束,都沒有接觸面向結構的數(shù)據(jù)庫設計;
          并完全在代碼中以面向對象的形式來設計的。


          hbn 的繼承映射關系有這幾種;
          1. 繼承關系類共同使用一個表;
          2. 每個子類一個表(mapping全);
          3. 每個具體內(nèi)一個表(mapping只有父類)。
          先說說3把,感覺這點最好用-》》
          繼承關系描述:
          ? Animals父類 -- Bird 、Reptiles;
          ? Animals.java
          package?TableBean;

          /** 動物父類
          ?*?@hibernate.class?
          ?*?table="animals"
          ?
          */
          public?class?Animals?{

          ????
          private?Integer?id?;
          ????
          private?Integer?size?;
          ????
          private?Integer?speed;
          ????
          /**
          ?????*?@hibernate.id
          ?????*?generator-class="increment"
          ?????
          */
          ????
          public?Integer?getId()?{
          ????????
          return?id;
          ????}

          ????
          public?void?setId(Integer?id)?{
          ????????
          this.id?=?id;
          ????}

          ????
          /** 動物體積大小
          ?????*?@hibernate.property
          ?????
          */
          ????
          public?Integer?getSize()?{
          ????????
          return?size;
          ????}

          ????
          public?void?setSize(Integer?size)?{
          ????????
          this.size?=?size;
          ????}
          ????
          /** 動物的移動速度
          ?????*?@hibernate.property
          ?????
          */
          ????
          public?Integer?getSpeed()?{
          ????????
          return?speed;
          ????}

          ????
          public?void?setSpeed(Integer?speed)?{
          ????????
          this.speed?=?speed;
          ????}
          ????
          }

          Bird.java
          package?TableBean;

          /** 在xdoeclt中聲明繼承表關系
          ?*?@hibernate.joined-subclass?
          ?*?@hibernate.joined-subclass-key?
          ?*??column="id"
          ?*?
          ?
          */
          public?class?Bird?extends?Animals?{
          ????
          private?String?wings?;
          ????
          private?Integer?leg?;
          ????
          /** 鳥類翅膀描述
          ?????*?@hibernate.property?
          ?????*?
          @return
          ?????
          */
          ????
          public?String?getWings()?{
          ????????
          return?wings;
          ????}

          ????
          public?void?setWings(String?wings)?{
          ????????
          this.wings?=?wings;
          ????}

          ????
          /**鳥腿描述
          ?????*?@hibernate.property?
          ?????*?
          @return
          ?????
          */
          ????
          public?Integer?getLeg()?{
          ????????
          return?leg;
          ????}

          ????
          public?void?setLeg(Integer?leg)?{
          ????????
          this.leg?=?leg;
          ????}
          ????
          }
          ?
          Reptiles.java
          package?TableBean;

          /** 在xdoeclt中聲明繼承表關系
          ?*?@hibernate.joined-subclass?
          ?*?@hibernate.joined-subclass-key?
          ?*??column="id"
          ?
          */
          public?class?Reptiles?extends?Animals?{
          ????
          private?Integer?leg?;
          ????
          /** 爬行動物腿個數(shù)
          ?????*?@hibernate.property?
          ?????*?
          @return
          ?????
          */
          ????
          public?Integer?getLeg()?{
          ????????
          return?leg;
          ????}

          ????
          public?void?setLeg(Integer?leg)?{
          ????????
          this.leg?=?leg;
          ????}
          ????
          }


          ant_xdoeclt.xml文件
          <?xml?version="1.0"?encoding="UTF-8"?>

          <!--?DO?NOT?EDIT?-->
          <!--?This?file?is?automatically?regenerated?for?each?new?XDoclet?generation.?-->
          <!--?Any?changes?you?make?to?this?file?will?be?overwritten.?If?you?want?-->
          <!--?to?edit?this?file,?make?a?copy?and?rename?it?for?your?own?use.?-->

          <project?name="Hbmzj?XDoclet?Generation"?default="_xdoclet_generation_"><property?file="xdoclet-build.properties"/>
          <property?name="xdoclet.basedir"?value="/C:/Program?Files/MyEclipse/eclipse/plugins/com.genuitec.jboss.ide.eclipse.xdoclet.core_4.0.0/"/>
          <path?id="xdoclet.classpath"><pathelement?location="E:/hibernate/Hbmzj/bin"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/rt.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/jsse.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/jce.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/charsets.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/ext/sunjce_provider.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/ext/sunpkcs11.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/ext/dnsns.jar"/>
          <pathelement?location="C:/Program?Files/Java/jdk1.5.0_02/jre/lib/ext/localedata.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/antlr-2.7.5H3.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/asm-attrs.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/asm.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/c3p0-0.8.5.2.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/cglib-2.1.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/cleanimports.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/commons-collections-2.1.1.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/commons-logging-1.0.4.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/concurrent-1.3.2.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/connector.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/dom4j-1.6.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/ehcache-1.1.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/hibernate3.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/jaas.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/jacc-1_0-fr.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/jaxen-1.1-beta-4.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/jdbc2_0-stdext.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/jgroups-2.2.7.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/jta.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/log4j-1.2.9.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/oscache-2.1.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/proxool-0.8.3.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/swarmcache-1.0rc2.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/xerces-2.6.2.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/xml-apis.jar"/>
          <pathelement?location="D:/eclipse2/eclipse/plugins/org.junit_3.8.1/junit.jar"/>
          <pathelement?location="E:/hibernate/Hbmzj/lib/MYSQL.JAR"/>

          <fileset?dir="${xdoclet.basedir}">
          <include?name="*.jar"/>
          <exclude?name="*xjavadoc*.jar"/>
          <include?name="jdk5/*.jar"/>
          </fileset>
          </path>
          <target?name="_xdoclet_generation_"?depends="N65540"/>
          <target?name="N65540"?description="Standard?Hibernate">
          <taskdef?name="hibernatedoclet"
          ???????? classname
          ="xdoclet.modules.hibernate.HibernateDocletTask"
          ???????? classpathref
          ="xdoclet.classpath"/>

          <
          hibernatedoclet?excludedTags="@version,@author,@todo,@see"?
          ?????????????? destDir
          ="src"?
          addedTags
          ="@xdoclet-generated?at?${TODAY},@copyright?The?XDoclet?Team,@author?XDoclet,@version?${version}"?>

          <fileset?dir="src"??includes="**/*.java"?>
          </fileset>

          <hibernate>
          </hibernate>
          </hibernatedoclet>
          </
          target>
          </
          project>

          ant運行 后得到 Animals.hbm.xml 文件
          使用 hbn工具 hbn2ddl 后
          mysql>?desc??Animals;
          +-------+---------+------+-----+---------+-------+
          |?Field?|?Type????|?Null?|?Key?|?Default?|?Extra?|
          +-------+---------+------+-----+---------+-------+
          |?id????|?int(11)?|??????|?PRI?|?0???????|???????|
          |?size??|?int(11)?|?YES??|?????|?NULL????|???????|
          |?speed?|?int(11)?|?YES??|?????|?NULL????|???????|
          +-------+---------+------+-----+---------+-------+
          3?rows?in?set?(0.00?sec)

          mysql
          >?desc?reptiles;
          +-------+---------+------+-----+---------+-------+
          |?Field?|?Type????|?Null?|?Key?|?Default?|?Extra?|
          +-------+---------+------+-----+---------+-------+
          |?id????|?int(11)?|??????|?PRI?|?0???????|???????|
          |?leg???|?int(11)?|?YES??|?????|?NULL????|???????|
          +-------+---------+------+-----+---------+-------+
          2?rows?in?set?(0.00?sec)

          mysql
          >?desc?bird;
          +-------+--------------+------+-----+---------+-------+
          |?Field?|?Type?????????|?Null?|?Key?|?Default?|?Extra?|
          +-------+--------------+------+-----+---------+-------+
          |?id????|?int(11)??????|??????|?PRI?|?0???????|???????|
          |?wings?|?varchar(255)?|?YES??|?????|?NULL????|???????|
          |?leg???|?int(11)??????|?YES??|?????|?NULL????|???????|
          +-------+--------------+------+-----+---------+-------+
          3?rows?in?set?(0.00?sec)


          添加測試數(shù)據(jù):
          mysql>?select?*?from?bird;
          +----+-------+------+
          |?id?|?wings?|?leg??|
          +----+-------+------+
          |??1?|?2G????|????2?|
          +----+-------+------+
          1?row?in?set?(0.00?sec)

          mysql
          >?select?*?from?Animals;
          +----+------+-------+
          |?id?|?size?|?speed?|
          +----+------+-------+
          |??1?|????1?|?????9?|
          |??2?|????6?|?????2?|
          +----+------+-------+
          2?rows?in?set?(0.00?sec)

          mysql
          >?select?*?from?Reptiles;
          +----+------+
          |?id?|?leg??|
          +----+------+
          |??2?|????4?|
          +----+------+
          1?row?in?set?(0.00?sec)



          測試
          public?class?TestJc?extends?TestCase?{
          ????
          public?void?testJC()?throws?Exception?{
          ????????Session?session
          =?HbnFactory.currentSession();
          ????????Transaction?tr?
          =?session.beginTransaction();
          ??????? //1
          ????????Animals?aa?
          =?(Animals)session.load(Animals.class,1);
          ????????System.out.println(aa);
          ??????? //2
          ????????Bird?bb?
          =??(Bird)?session.load(Bird.class,1);
          ????????System.out.println(bb);
          ??????? //3
          ????????Object?obj?
          =?session.createQuery("?from?Animals?").list();
          ????????System.out.println(obj);
          ????????
          ????????tr.commit();
          ????????HbnFactory.closeSession();
          ????}
          }
          結果:
          //1
          Hibernate:?
          select?animals0_.id?as?id0_,?animals0_.size?as?size0_0_,?animals0_.speed?as?speed0_0_,?animals0_1_.wings?as?wings1_0_,?animals0_1_.leg?as?leg1_0_,?animals0_2_.leg?as?leg2_0_,?case?when?animals0_1_.id?is?not?null?then?1?when?animals0_2_.id?is?not?null?then?2?when?animals0_.id?is?not?null?then?0?end?as?clazz_0_?from?animals?animals0_?left?outer?join?Bird?animals0_1_?on?animals0_.id=animals0_1_.id?left?outer?join?Reptiles?animals0_2_?on?animals0_.id=animals0_2_.id?where?animals0_.id=?
          TableBean.Bird
          @5e176f
          //2
          TableBean.Bird
          @5e176f
          //3
          Hibernate:?
          select?animals0_.id?as?id,?animals0_.size?as?size0_,?animals0_.speed?as?speed0_,?animals0_1_.wings?as?wings1_,?animals0_1_.leg?as?leg1_,?animals0_2_.leg?as?leg2_,?case?when?animals0_1_.id?is?not?null?then?1?when?animals0_2_.id?is?not?null?then?2?when?animals0_.id?is?not?null?then?0?end?as?clazz_?from?animals?animals0_?left?outer?join?Bird?animals0_1_?on?animals0_.id=animals0_1_.id?left?outer?join?Reptiles?animals0_2_?on?animals0_.id=animals0_2_.id
          [TableBean.Bird@5e176f,?TableBean.Reptiles@2c1e6b,?TableBean.Reptiles@811c88]



          主站蜘蛛池模板: 南和县| 临桂县| 固安县| 晴隆县| 平邑县| 博客| 宝山区| 华亭县| 开原市| 厦门市| 满洲里市| 永修县| 偏关县| 东至县| 道真| 天柱县| 肇州县| 克什克腾旗| 广安市| 石狮市| 横山县| 寿光市| 仁怀市| 平山县| 靖安县| 白水县| 花莲市| 磐安县| 浦县| 哈密市| 涿鹿县| 鸡西市| 吉安县| 尤溪县| 万年县| 苏尼特左旗| 古丈县| 东至县| 高邑县| 桐梓县| 博罗县|