Dev@Free

          zJun's Tech Weblog

          [Hibernate Annotations] 主鍵映射

          1.簡單主鍵:
          使用
          @Id 注解可以將實體bean中的某個屬性定義為標識字段.使用 @GeneratedValue 注解可以定義標識字段的生成策略:

          AUTO - 可以是identity類型的字段,或者sequence類型或者table類型,取決于不同的底層數據庫.
          TABLE - 使用表保存id值
          IDENTITY - identity字段
          SEQUENCE - sequence

          @Id?@GeneratedValue(strategy = GenerationType.IDENTITY)
          public ?Long?getId()?{??}

          2.組合主鍵:

          定義組合主鍵的幾種語法:
          • 將組件類注解為@Embeddable,并將組件的屬性注解為@Id
          • 將組件的屬性注解為@EmbeddedId
          • 將類注解為@IdClass,并將該實體中所有屬于主鍵的屬性都注解為@Id
          使用 @IdClass 組合來定義組合主鍵類 , 對應了一個實體類中的多個字段或屬性, 而且主鍵類中用于定義主鍵的字段或屬性和實體類中對應的字段或屬性在類型上必須一致.

          @Entity
          @IdClass(FootballerPk.
          class )? // ?指明主鍵類
          public ? class ?Footballer?{
          // part?of?the?id?key
          @Id? public ?String?getFirstname()?{
          return ?firstname;
          }

          public ? void ?setFirstname(String?firstname)?{
          this .firstname? = ?firstname;
          }

          // part?of?the?id?key
          @Id? public ?String?getLastname()?{
          return ?lastname;
          }

          public ? void ?setLastname(String?lastname)?{
          this .lastname? = ?lastname;
          }

          public ?String?getClub()?{
          return ?club;
          }

          public ? void ?setClub(String?club)?{
          this .club? = ?club;
          }

          // appropriate?equals()?and?hashCode()?implementation
          }

          @Embeddable??
          // ?主鍵類
          public ? class ?FootballerPk? implements ?Serializable?{
          // same?name?and?type?as?in?Footballer
          public ?String?getFirstname()?{
          return ?firstname;
          }

          public ? void ?setFirstname(String?firstname)?{
          this .firstname? = ?firstname;
          }

          // same?name?and?type?as?in?Footballer
          public ?String?getLastname()?{
          return ?lastname;
          }

          public ? void ?setLastname(String?lastname)?{
          this .lastname? = ?lastname;
          }

          // appropriate?equals()?and?hashCode()?implementation
          }


          posted on 2007-03-07 15:49 zJun's帛羅閣 閱讀(2557) 評論(0)  編輯  收藏 所屬分類: 開源軟件

          導航

          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(15)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          博客

          文檔

          站點

          論壇

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 通海县| 中山市| 南昌市| 唐河县| 吐鲁番市| 丹寨县| 彰武县| 大方县| 延川县| 松桃| 满洲里市| 本溪| 虎林市| 西林县| 平舆县| 扶风县| 阿合奇县| 永善县| 德庆县| 福建省| 乌拉特后旗| 郑州市| 浮梁县| 阿图什市| 晋州市| 旺苍县| 牡丹江市| 子长县| 石首市| 洛南县| 虎林市| 正阳县| 塘沽区| 即墨市| 平乐县| 饶平县| 丰宁| 华亭县| 望江县| 丰城市| 奉化市|