千里冰封
          JAVA 濃香四溢
          posts - 151,comments - 2801,trackbacks - 0
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          盡管千里冰封
          依然擁有晴空

           

          留言簿(204)

          隨筆分類(197)

          隨筆檔案(189)

          文章檔案(2)

          友情鏈接

          搜索

          •  

          積分與排名

          • 積分 - 965861
          • 排名 - 35

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          以前看到一本書(shū)上寫(xiě)的,有關(guān)定義實(shí)體BEAN的一些細(xì)節(jié),直到今天才知道其中的差別

          代碼1:

          /*   
           * Test.java   
           *   
           * Created on 2006年12月15日, 上午12:06   
           *   
           * To change this template, choose Tools | Template Manager   
           * and open the template in the editor.   
           
          */   
             
          package com.hadeslee.entity;    
             
          import java.io.Serializable;    
          import javax.persistence.Entity;    
          import javax.persistence.GeneratedValue;    
          import javax.persistence.GenerationType;    
          import javax.persistence.Id;    
             
          /**   
           * Entity class Test   
           *    
           * 
          @author lbf   
           
          */   
          @Entity   
          public class Test implements Serializable {    
              
          private Long id;    
              
          private String name,sex,age;    
              
          private int idCard;    
              
          /** Creates a new instance of Test */   
              
          public Test() {    
              }    
             
              
          /**   
               * Gets the id of this Test.   
               * 
          @return the id   
               
          */   
              @Id   
              @GeneratedValue(strategy 
          = GenerationType.AUTO)    
              
          public Long getId() {    
                  
          return this.id;    
              }    
             
              
          /**   
               * Sets the id of this Test to the specified value.   
               * 
          @param id the new id   
               
          */   
              
          public void setId(Long id) {    
                  
          this.id = id;    
              }    
              
          public void setNameID(int ids){    
                  
          this.idCard=ids;    
              }    
              
          public int getNameID(){    
                  
          return idCard;    
              }    
             
              
          /**   
               * Returns a hash code value for the object.  This implementation computes    
               * a hash code value based on the id fields in this object.   
               * 
          @return a hash code value for this object.   
               
          */   
              @Override   
              
          public int hashCode() {    
                  
          int hash = 0;    
                  hash 
          += (this.id != null ? this.id.hashCode() : 0);    
                  
          return hash;    
              }    
             
              
          /**   
               * Determines whether another object is equal to this Test.  The result is    
               * true if and only if the argument is not null and is a Test object that    
               * has the same id field values as this object.   
               * 
          @param object the reference object with which to compare   
               * 
          @return true if this object is the same as the argument;   
               * false otherwise.   
               
          */   
              @Override   
              
          public boolean equals(Object object) {    
                  
          // TODO: Warning - this method won't work in the case the id fields are not set    
                  if (!(object instanceof Test)) {    
                      
          return false;    
                  }    
                  Test other 
          = (Test)object;    
                  
          if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;    
                  
          return true;    
              }    
             
              
          /**   
               * Returns a string representation of the object.  This implementation constructs    
               * that representation based on the id fields.   
               * 
          @return a string representation of the object.   
               
          */   
              @Override   
              
          public String toString() {    
                  
          return "com.hadeslee.entity.Test[id=" + id + "]";    
              }    
             
              
          public String getName() {    
                  
          return name;    
              }    
             
              
          public void setName(String name) {    
                  
          this.name = name;    
              }    
             
              
          public String getSex() {    
                  
          return sex;    
              }    
             
              
          public void setSex(String sex) {    
                  
          this.sex = sex;    
              }    
             
              
          public String getAge() {    
                  
          return age;    
              }    
             
              
          public void setAge(String age) {    
                  
          this.age = age;    
              }    
                  
          }    

          代碼2:

           

          /*   
           * Test.java   
           *   
           * Created on 2006年12月15日, 上午12:06   
           *   
           * To change this template, choose Tools | Template Manager   
           * and open the template in the editor.   
           
          */   
             
          package com.hadeslee.entity;    
             
          import java.io.Serializable;    
          import javax.persistence.Entity;    
          import javax.persistence.GeneratedValue;    
          import javax.persistence.GenerationType;    
          import javax.persistence.Id;    
             
          /**   
           * Entity class Test   
           *    
           * 
          @author lbf   
           
          */   
          @Entity   
          public class Test implements Serializable {    
              @Id   
              @GeneratedValue(strategy 
          = GenerationType.AUTO)    
              
          private Long id;    
              
          private String name,sex,age;    
              
          private int idCard;    
              
          /** Creates a new instance of Test */   
              
          public Test() {    
              }    
             
              
          /**   
               * Gets the id of this Test.   
               * 
          @return the id   
               
          */   
                 
              
          public Long getId() {    
                  
          return this.id;    
              }    
             
              
          /**   
               * Sets the id of this Test to the specified value.   
               * 
          @param id the new id   
               
          */   
              
          public void setId(Long id) {    
                  
          this.id = id;    
              }    
              
          public void setNameID(int ids){    
                  
          this.idCard=ids;    
              }    
              
          public int getNameID(){    
                  
          return idCard;    
              }    
             
              
          /**   
               * Returns a hash code value for the object.  This implementation computes    
               * a hash code value based on the id fields in this object.   
               * 
          @return a hash code value for this object.   
               
          */   
              @Override   
              
          public int hashCode() {    
                  
          int hash = 0;    
                  hash 
          += (this.id != null ? this.id.hashCode() : 0);    
                  
          return hash;    
              }    
             
              
          /**   
               * Determines whether another object is equal to this Test.  The result is    
               * true if and only if the argument is not null and is a Test object that    
               * has the same id field values as this object.   
               * 
          @param object the reference object with which to compare   
               * 
          @return true if this object is the same as the argument;   
               * false otherwise.   
               
          */   
              @Override   
              
          public boolean equals(Object object) {    
                  
          // TODO: Warning - this method won't work in the case the id fields are not set    
                  if (!(object instanceof Test)) {    
                      
          return false;    
                  }    
                  Test other 
          = (Test)object;    
                  
          if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;    
                  
          return true;    
              }    
             
              
          /**   
               * Returns a string representation of the object.  This implementation constructs    
               * that representation based on the id fields.   
               * 
          @return a string representation of the object.   
               
          */   
              @Override   
              
          public String toString() {    
                  
          return "com.hadeslee.entity.Test[id=" + id + "]";    
              }    
             
              
          public String getName() {    
                  
          return name;    
              }    
             
              
          public void setName(String name) {    
                  
          this.name = name;    
              }    
             
              
          public String getSex() {    
                  
          return sex;    
              }    
             
              
          public void setSex(String sex) {    
                  
          this.sex = sex;    
              }    
             
              
          public String getAge() {    
                  
          return age;    
              }    
             
              
          public void setAge(String age) {    
                  
          this.age = age;    
              }    
                  
          }    


          代碼1和代碼2唯一的差別就是@Id的注釋地方不同了

          同樣是注釋主鍵,當(dāng)在直接用在變量上注釋時(shí),如果其它的成員變量沒(méi)有指定名字,則數(shù)據(jù)庫(kù)生成的表的各列名字將以定義的成員變量的變量名為準(zhǔn)

          當(dāng)用在getter方法注釋時(shí),則數(shù)據(jù)庫(kù)生成的表的各列名字將取getXXXX的XXXX名字,將不再取定義的成員變量名

          像上面的例子中,代碼1會(huì)有IdCard這一列,則代碼2取而代之的將是NameID這一列.這看上去是一個(gè)小小的差別,但是了解了終究是好事.呵呵.終于懂清楚在get上注釋和直接在成員變量上注釋的差別了,一般來(lái)說(shuō)是不會(huì)有什么差別的,一般標(biāo)準(zhǔn) 的JAVABEAN都是成員變量名和getter,setter簽名一樣的.




          盡管千里冰封
          依然擁有晴空

          你我共同品味JAVA的濃香.
          posted on 2007-09-11 15:45 千里冰封 閱讀(986) 評(píng)論(0)  編輯  收藏 所屬分類: JAVAEE
          主站蜘蛛池模板: 廊坊市| 高清| 万全县| 资溪县| 杭州市| 景谷| 锡林郭勒盟| 阿尔山市| 海门市| 宁安市| 绍兴县| 文登市| 民权县| 阿合奇县| 宜黄县| 浦东新区| 连城县| 肃南| 南投县| 南宁市| 武宁县| 定边县| 保康县| 乌拉特后旗| 安远县| 梁河县| 广昌县| 萨嘎县| 鹰潭市| 文山县| 保定市| 庆云县| 沙坪坝区| 依兰县| 潜山县| 分宜县| 花垣县| 隆安县| 汝州市| 从江县| 石渠县|