posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          EJB初學日記(5)

          Posted on 2007-09-18 21:30 semovy 閱讀(318) 評論(0)  編輯  收藏 所屬分類: EJB

          以前看到一本書上寫的,有關定義實體BEAN的一些細節,直到今天才知道其中的差別

          代碼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的注釋地方不同了

          同樣是注釋主鍵,當在直接用在變量上注釋時,如果其它的成員變量沒有指定名字,則數據庫生成的表的各列名字將以定義的成員變量的變量名為準

          當用在getter方法注釋時,則數據庫生成的表的各列名字將取getXXXX的XXXX名字,將不再取定義的成員變量名

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

          主站蜘蛛池模板: 通山县| 团风县| 盘山县| 苍山县| 吴旗县| 关岭| 荔波县| 涿州市| 鄂温| 剑川县| 长沙县| 莱芜市| 安国市| 云南省| 芮城县| 通城县| 鹿泉市| 淮北市| 延长县| 长海县| 隆安县| 广河县| 本溪市| 吉首市| 榆中县| 左云县| 浦江县| 永修县| 玉林市| 布拖县| 永福县| 荔浦县| 合川市| 惠东县| 洛南县| 余庆县| 洛隆县| 永德县| 武平县| 丰镇市| 堆龙德庆县|