隨筆-124  評論-49  文章-56  trackbacks-0

          Component映射(值對象映射)

          在hibernate中,component是某個實體的邏輯組成部分,它與實體的根本區別是沒有oid,
          component可以稱為是值對象(DDD)

          采用component映射的好處:它實現了對象模型的細粒度劃分,層次會更分明,復用率會更高

          <!--
            User:                             Comtact:
            private int id;                    private String email;
              private String name;         private String address;
              private Comtact comtact;     private String phone;
          -->
          <class name="User" table="t_user">
                  
          <id name="id">
                      
          <generator class="native"/>
                  
          </id>
                  
          <property name="name"/>
                  
          <component name="comtact">
                      
          <property name="email"/>
                      
          <property name="address"/>
                      
          <property name="phone"/>
                  
          </component>
          </class>

          復合(聯合)主鍵映射

          通常將復合主鍵相關的屬性,單獨放到一個類中
           * 此類必須實現序列化接口
           * 覆寫hashcode和equals方法

          <class name="com.bjsxt.hibernate.FiscalYearPeriod" table="t_fiscal_year_period">
                  
          <composite-id name="fiscalYearPeriodPK">
                      
          <key-property name="fiscalYear"/>
                      
          <key-property name="fiscalPeriod"/>    
                  
          </composite-id>
                  
          <property name="beginDate"/>
                  
          <property name="endDate"/>
                  
          <property name="periodSts"/>
              
          </class>

          public class FiscalYearPeriodPK implements Serializable {
              
          //核算年
              private int fiscalYear;
              
          //核算月
              private int fiscalPeriod;
              
          public int getFiscalYear() {
                  
          return fiscalYear;
              }

              
          public void setFiscalYear(int fiscalYear) {
                  
          this.fiscalYear = fiscalYear;
              }

              
          public int getFiscalPeriod() {
                  
          return fiscalPeriod;
              }

              
          public void setFiscalPeriod(int fiscalPeriod) {
                  
          this.fiscalPeriod = fiscalPeriod;
              }


              @Override
              
          public int hashCode() {
                  
          final int prime = 31;
                  
          int result = 1;
                  result 
          = prime * result + fiscalPeriod;
                  result 
          = prime * result + fiscalYear;
                  
          return result;
              }


              @Override
              
          public boolean equals(Object obj) {
                  
          if (this == obj)
                      
          return true;
                  
          if (obj == null)
                      
          return false;
                  
          if (getClass() != obj.getClass())
                      
          return false;
                  
          final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
                  
          if (fiscalPeriod != other.fiscalPeriod)
                      
          return false;
                  
          if (fiscalYear != other.fiscalYear)
                      
          return false;
                  
          return true;
              }

          }


          public class FiscalYearPeriod {
              
          private FiscalYearPeriodPK fiscalYearPeriodPK;
              
          //開始日期
              private Date beginDate;
              
          //結束日期
              private Date endDate;
              
          //狀態
              private String periodSts;
          }
          posted on 2009-11-03 16:22 junly 閱讀(200) 評論(0)  編輯  收藏 所屬分類: hibernate/orm
          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          other

          文章

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 惠来县| 连山| 中西区| 桂林市| 东安县| 即墨市| 宝兴县| 承德县| 临桂县| 康定县| 云林县| 邳州市| 芮城县| 顺义区| 灵石县| 手机| 武川县| 蓝田县| 开远市| 巴塘县| 奉新县| 兴化市| 白水县| 长宁县| 奉化市| 扎赉特旗| 辽阳市| 泾川县| 渑池县| 崇文区| 龙门县| 尖扎县| 磐石市| 茌平县| 合水县| 黎城县| 海伦市| 金乡县| 景泰县| 丰原市| 阿巴嘎旗|