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

          EJB初學(xué)日記(5)

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

          以前看到一本書上寫的,有關(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í),如果其它的成員變量沒有指定名字,則數(shù)據(jù)庫生成的表的各列名字將以定義的成員變量的變量名為準(zhǔn)

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

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

          主站蜘蛛池模板: 盘山县| 湖州市| 大埔区| 积石山| 普安县| 湾仔区| 长葛市| 当阳市| 含山县| 嘉祥县| 慈溪市| 安庆市| 阿荣旗| 拉萨市| 怀来县| 精河县| 宁乡县| 遵义市| 齐齐哈尔市| 海原县| 伊宁市| 玉环县| 西乌| 新乡市| 抚宁县| 柳江县| 新平| 富裕县| 万盛区| 巩留县| 清苑县| 丽水市| 新田县| 固镇县| 天镇县| 汪清县| 济宁市| 光泽县| 巴林左旗| 利辛县| 黄大仙区|