一点小小的细节

今天看到一本书上写的,有关定义实体BEAN的一些细节,直到今天才知道其中的差别

代码如下:

java 代码1
  1. /*  
  2.  * Test.java  
  3.  *  
  4.  * Created on 2006年12月15日, 上午12:06  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package com.hadeslee.entity;   
  11.   
  12. import java.io.Serializable;   
  13. import javax.persistence.Entity;   
  14. import javax.persistence.GeneratedValue;   
  15. import javax.persistence.GenerationType;   
  16. import javax.persistence.Id;   
  17.   
  18. /**  
  19.  * Entity class Test  
  20.  *   
  21.  * @author lbf  
  22.  */  
  23. @Entity  
  24. public class Test implements Serializable {   
  25.     private Long id;   
  26.     private String name,sex,age;   
  27.     private int idCard;   
  28.     /** Creates a new instance of Test */  
  29.     public Test() {   
  30.     }   
  31.   
  32.     /**  
  33.      * Gets the id of this Test.  
  34.      * @return the id  
  35.      */  
  36.     @Id  
  37.     @GeneratedValue(strategy = GenerationType.AUTO)   
  38.     public Long getId() {   
  39.         return this.id;   
  40.     }   
  41.   
  42.     /**  
  43.      * Sets the id of this Test to the specified value.  
  44.      * @param id the new id  
  45.      */  
  46.     public void setId(Long id) {   
  47.         this.id = id;   
  48.     }   
  49.     public void setNameID(int ids){   
  50.         this.idCard=ids;   
  51.     }   
  52.     public int getNameID(){   
  53.         return idCard;   
  54.     }   
  55.   
  56.     /**  
  57.      * Returns a hash code value for the object.  This implementation computes   
  58.      * a hash code value based on the id fields in this object.  
  59.      * @return a hash code value for this object.  
  60.      */  
  61.     @Override  
  62.     public int hashCode() {   
  63.         int hash = 0;   
  64.         hash += (this.id != null ? this.id.hashCode() : 0);   
  65.         return hash;   
  66.     }   
  67.   
  68.     /**  
  69.      * Determines whether another object is equal to this Test.  The result is   
  70.      * <code>true</code> if and only if the argument is not null and is a Test object that   
  71.      * has the same id field values as this object.  
  72.      * @param object the reference object with which to compare  
  73.      * @return <code>true</code> if this object is the same as the argument;  
  74.      * <code>false</code> otherwise.  
  75.      */  
  76.     @Override  
  77.     public boolean equals(Object object) {   
  78.         // TODO: Warning - this method won't work in the case the id fields are not set   
  79.         if (!(object instanceof Test)) {   
  80.             return false;   
  81.         }   
  82.         Test other = (Test)object;   
  83.         if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;   
  84.         return true;   
  85.     }   
  86.   
  87.     /**  
  88.      * Returns a string representation of the object.  This implementation constructs   
  89.      * that representation based on the id fields.  
  90.      * @return a string representation of the object.  
  91.      */  
  92.     @Override  
  93.     public String toString() {   
  94.         return "com.hadeslee.entity.Test[id=" + id + "]";   
  95.     }   
  96.   
  97.     public String getName() {   
  98.         return name;   
  99.     }   
  100.   
  101.     public void setName(String name) {   
  102.         this.name = name;   
  103.     }   
  104.   
  105.     public String getSex() {   
  106.         return sex;   
  107.     }   
  108.   
  109.     public void setSex(String sex) {   
  110.         this.sex = sex;   
  111.     }   
  112.   
  113.     public String getAge() {   
  114.         return age;   
  115.     }   
  116.   
  117.     public void setAge(String age) {   
  118.         this.age = age;   
  119.     }   
  120.        
  121. }   
java 代码2
  1. /*  
  2.  * Test.java  
  3.  *  
  4.  * Created on 2006年12月15日, 上午12:06  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package com.hadeslee.entity;   
  11.   
  12. import java.io.Serializable;   
  13. import javax.persistence.Entity;   
  14. import javax.persistence.GeneratedValue;   
  15. import javax.persistence.GenerationType;   
  16. import javax.persistence.Id;   
  17.   
  18. /**  
  19.  * Entity class Test  
  20.  *   
  21.  * @author lbf  
  22.  */  
  23. @Entity  
  24. public class Test implements Serializable {   
  25.     @Id  
  26.     @GeneratedValue(strategy = GenerationType.AUTO)   
  27.     private Long id;   
  28.     private String name,sex,age;   
  29.     private int idCard;   
  30.     /** Creates a new instance of Test */  
  31.     public Test() {   
  32.     }   
  33.   
  34.     /**  
  35.      * Gets the id of this Test.  
  36.      * @return the id  
  37.      */  
  38.       
  39.     public Long getId() {   
  40.         return this.id;   
  41.     }   
  42.   
  43.     /**  
  44.      * Sets the id of this Test to the specified value.  
  45.      * @param id the new id  
  46.      */  
  47.     public void setId(Long id) {   
  48.         this.id = id;   
  49.     }   
  50.     public void setNameID(int ids){   
  51.         this.idCard=ids;   
  52.     }   
  53.     public int getNameID(){   
  54.         return idCard;   
  55.     }   
  56.   
  57.     /**  
  58.      * Returns a hash code value for the object.  This implementation computes   
  59.      * a hash code value based on the id fields in this object.  
  60.      * @return a hash code value for this object.  
  61.      */  
  62.     @Override  
  63.     public int hashCode() {   
  64.         int hash = 0;   
  65.         hash += (this.id != null ? this.id.hashCode() : 0);   
  66.         return hash;   
  67.     }   
  68.   
  69.     /**  
  70.      * Determines whether another object is equal to this Test.  The result is   
  71.      * <code>true</code> if and only if the argument is not null and is a Test object that   
  72.      * has the same id field values as this object.  
  73.      * @param object the reference object with which to compare  
  74.      * @return <code>true</code> if this object is the same as the argument;  
  75.      * <code>false</code> otherwise.  
  76.      */  
  77.     @Override  
  78.     public boolean equals(Object object) {   
  79.         // TODO: Warning - this method won't work in the case the id fields are not set   
  80.         if (!(object instanceof Test)) {   
  81.             return false;   
  82.         }   
  83.         Test other = (Test)object;   
  84.         if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;   
  85.         return true;   
  86.     }   
  87.   
  88.     /**  
  89.      * Returns a string representation of the object.  This implementation constructs   
  90.      * that representation based on the id fields.  
  91.      * @return a string representation of the object.  
  92.      */  
  93.     @Override  
  94.     public String toString() {   
  95.         return "com.hadeslee.entity.Test[id=" + id + "]";   
  96.     }   
  97.   
  98.     public String getName() {   
  99.         return name;   
  100.     }   
  101.   
  102.     public void setName(String name) {   
  103.         this.name = name;   
  104.     }   
  105.   
  106.     public String getSex() {   
  107.         return sex;   
  108.     }   
  109.   
  110.     public void setSex(String sex) {   
  111.         this.sex = sex;   
  112.     }   
  113.   
  114.     public String getAge() {   
  115.         return age;   
  116.     }   
  117.   
  118.     public void setAge(String age) {   
  119.         this.age = age;   
  120.     }   
  121.        
  122. }   

代码1和代码2唯一的差别就是@Id的注释地方不同了

同样是注释主键,当在直接用在变量上注释时,如果其它的成员变量没有指定名字,则数据库生成的表的各列名字将以定义的成员变量的变量名为准

当用在getter方法注释时,则数据库生成的表的各列名字将取getXXXX的XXXX名字,将不再取定义的成员变量名

像上面的例子中,代码1会有IdCard这一列,则代码2取而代之的将是NameID这一列.这看上去是一个小小的差别,但是了解了终究是好事.呵呵.终于懂清楚在get上注释和直接在成员变量上注释的差别了,一般来说是不会有什么差别的,一般标准 的JAVABEAN都是成员变量名和getter,setter签名一样的.

好了,睡觉去了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值