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) 評論(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ā)出一個全新的投資計算器以展示如何使用實體BEAN,這個新的投資計算器允許你在關(guān)系數(shù)據(jù)庫中保存一個fund company profiles列表和一個individual investor profiles列表。對于每一次的投資計算,你都可以從DB中選擇一個company和一個individual,然后輸入每月的存款來計算最終的投資回報。每一次的計算記錄用一個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體系來模擬上面所說的投資計算器。它們都遵循簡單的javabean規(guī)范(setter, getter).
          relationship.png
          Fund類表示profiles of available investment funds。它有一個name和一個average annual growth rate屬性

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

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

          TimedRecord類表示一次以time stamp標(biāo)記的calculation record, 它繼承自Record類,并增加了一個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自動生成。
          你可以顯示的給每一個屬性指定它所對應(yīng)的column,column的默認(rèn)值同屬性的名字。
          生成的schema如下圖所示:
          fund.png
          Investor類與Fund類似,這里不再表述。

          Record實體bean比較Fund和Investor都要復(fù)雜,它不僅包括簡單的數(shù)據(jù)屬性,還包括其它bean(比如Fund和Investor)自身,為了表達(dá)它們之間的關(guān)系,在Record表中含有對應(yīng)的外鍵(關(guān)聯(lián)到Fund和Investor的主鍵)。你可以在Record Class中使用標(biāo)注@JoinColumn來表示這些外鍵,如果你忘了寫,EJB 3.0 server會給你指定一個默認(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)系。比如,你可以給繼承體系中的每一個類一個獨立的表然后使用外鍵來約束它們之間的關(guān)系。在這個示例中,我們使用另外一種映射策略---單表映射策略。

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

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

          TimedRecord 類簡單的繼承自 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;
          ??}
          }

          主站蜘蛛池模板: 祁门县| 开阳县| 舞钢市| 宁波市| 水城县| 尼玛县| 阿瓦提县| 教育| 长白| 普定县| 兴化市| 神池县| 和硕县| 中方县| 余姚市| 邢台市| 青铜峡市| 昌宁县| 铁岭市| 北京市| 方城县| 盐边县| 读书| 福建省| 德钦县| 天水市| 霍林郭勒市| 林口县| 临武县| 潼南县| 牟定县| 遵义县| 陆丰市| 华坪县| 郧西县| 登封市| 伊金霍洛旗| 集贤县| 稷山县| 张掖市| 星子县|