Let's go inside

          this blog is deprecated as a result of laziness.
          posts - 59, comments - 2, trackbacks - 0, articles - 0

          TrailBlazer第10天--OR Mapping with Entity Bean

          Posted on 2006-07-28 02:21 Earth 閱讀(241) 評(píng)論(0)  編輯  收藏 所屬分類: JavaEE5/EJB3

          ? In the next several trails, we develop a new investment calculator application to demonstrate how to use EJB 3.0 entity beans. The new investment calculator allows you to save a list of fund company profiles and a list of individual investor profiles in a relational database. For each investment calculation, you can choose a company and an individual from the database, and then enter a monthly saving amount to calculate the final investment return. Each calculation record is also saved in the database with a time stamp. We can later search and update the calculation records in the database.
          在下面的幾節(jié)中,我們將開發(fā)出一個(gè)全新的投資計(jì)算器以展示如何使用實(shí)體BEAN,這個(gè)新的投資計(jì)算器允許你在關(guān)系數(shù)據(jù)庫(kù)中保存一個(gè)fund company profiles列表和一個(gè)individual investor profiles列表。對(duì)于每一次的投資計(jì)算,你都可以從DB中選擇一個(gè)company和一個(gè)individual,然后輸入每月的存款來計(jì)算最終的投資回報(bào)。每一次的計(jì)算記錄用一個(gè)time stamp保存到DB中。這樣,我們可以在以后search和update這些記錄。

          We construct the following object class hierarchy to model the data in the above investment calculator application. All classes conform to simple JavaBeans convention (i.e., standard getter and setter methods for data attributes).
          我們將構(gòu)建如下的object class體系來模擬上面所說的投資計(jì)算器。它們都遵循簡(jiǎn)單的javabean規(guī)范(setter, getter).
          relationship.png
          Fund類表示profiles of available investment funds。它有一個(gè)name和一個(gè)average annual growth rate屬性

          Investor類表示profiles of individual investors.它有如下的數(shù)據(jù)屬性:投資者的name, 投資的start age, 投資的end age

          Record類表示一次investment calculation。它有如下數(shù)據(jù)屬性:一個(gè)Fund,一個(gè)Investor,一個(gè)monthly saving amount, 還有一個(gè)final result.

          TimedRecord類表示一次以time stamp標(biāo)記的calculation record, 它繼承自Record類,并增加了一個(gè)timestamp類型的屬性。

          @Entity
          @Table(name?
          = ? " fund " )
          public ? class ?Fund? implements ?Serializable?{
          ??
          private ? int ?id;
          ??
          private ?String?name;
          ??
          private ? double ?growthrate;

          ??
          public ?Fund?()?{?}

          ??
          public ?Fund?(String?name,? double ?growthrate)?{
          ????
          this .name? = ?name;
          ????
          this .growthrate? = ?growthrate;
          ??}

          ??@Id
          ??@GeneratedValue
          ??
          public ? int ?getId?()?{
          ????
          return ?id;
          ??}

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

          ??
          public ?String?getName?()?{
          ????
          return ?name;
          ??}

          ??
          public ? void ?setName?(String?name)?{
          ????
          this .name? = ?name;
          ??}
          ?
          // ?Other?getter?and?setter?methods?
          }


          @Id 表示Id是表的主鍵
          @GeneratedValue表示主鍵的值由Server自動(dòng)生成。
          你可以顯示的給每一個(gè)屬性指定它所對(duì)應(yīng)的column,column的默認(rèn)值同屬性的名字。
          生成的schema如下圖所示:
          fund.png
          Investor類與Fund類似,這里不再表述。

          Record實(shí)體bean比較Fund和Investor都要復(fù)雜,它不僅包括簡(jiǎn)單的數(shù)據(jù)屬性,還包括其它bean(比如Fund和Investor)自身,為了表達(dá)它們之間的關(guān)系,在Record表中含有對(duì)應(yīng)的外鍵(關(guān)聯(lián)到Fund和Investor的主鍵)。你可以在Record Class中使用標(biāo)注@JoinColumn來表示這些外鍵,如果你忘了寫,EJB 3.0 server會(huì)給你指定一個(gè)默認(rèn)的column.


          標(biāo)注還可以用來表示外鍵之間的約束關(guān)系。比如
          @ManyToOne
          @OneToMany
          @OneToOne:
          @ManyToMany

          @Entity
          @Table(name?
          = ? " record " )
          // ??
          public ? class ?Record? implements ?Serializable?{
          ??
          protected ? int ?id;
          ??
          protected ?Fund?fund;
          ??
          protected ?Investor?investor;
          ??
          protected ? double ?saving;
          ??
          protected ? double ?result;

          ??
          // ??
          ??@ManyToOne(optional = false )
          ??@JoinColumn(name
          = " my_fundid " )
          ??
          public ?Fund?getFund?()?{
          ????
          return ?fund;
          ??}

          ??@ManyToOne(optional
          = false )
          ??
          // ?Use?the?system-specified?join?column
          ?? public ?Investor?getInvestor?()?{
          ????
          return ?investor;
          ??}
          ??
          // ?Other?getter?and?setter?methods?
          }

          TimedRecord類繼承Record,有多種方式映射繼承關(guān)系。比如,你可以給繼承體系中的每一個(gè)類一個(gè)獨(dú)立的表然后使用外鍵來約束它們之間的關(guān)系。在這個(gè)示例中,我們使用另外一種映射策略---單表映射策略。

          single table mapping strategy是EJB3.0中默認(rèn)的繼承映射策略。它把基類Record和子類TimedRecord映射到同一張表。這張表中含有所有類中的所有屬性。用一個(gè)特殊的列來區(qū)分是哪一個(gè)子類。
          必須在基類中指定Mapping Strategy和differentiator column !我們分別用@DiscriminatorColumn 和 @DiscriminatorValue來標(biāo)注,在這個(gè)例子中,Record表中的 record_type表示 differentiator, 如果它的值是B,則表示它是基類Record

          @Entity
          @Table(name?
          = ? " record " )
          @DiscriminatorColumn(name
          = " record_type " )
          @DiscriminatorValue(value
          = " B " )
          public ? class ?Record?{
          ????
          // ??
          }

          TimedRecord 類簡(jiǎn)單的繼承自 Record class.它的@DiscriminatorValue表明所有的TimedRecord在record_type這一列上的值為T

          @Entity
          @DiscriminatorValue?(value
          = " T " )
          public ? class ?TimedRecord? extends ?Record?{
          ??
          private ?Timestamp?ts;
          ??
          // ??
          ?? public ?Timestamp?getTs?()?{
          ????
          return ?ts;
          ??}
          ??
          public ? void ?setTs?(Timestamp?ts)?{
          ????
          this .ts? = ?ts;
          ??}
          }

          主站蜘蛛池模板: 志丹县| 高要市| 襄垣县| 焉耆| 米林县| 盖州市| 樟树市| 获嘉县| 上蔡县| 海安县| 通道| 罗定市| 开江县| 额济纳旗| 淅川县| 嫩江县| 叶城县| 三明市| 皮山县| 镇平县| 神木县| 金坛市| 禹州市| 藁城市| 灌南县| 宜春市| 常宁市| 麻阳| 新乐市| 任丘市| 孟津县| 武隆县| 永川市| 垦利县| 益阳市| 靖江市| 怀来县| 巴中市| 吉水县| 台北县| 三门峡市|