`
lbfhappy
  • 浏览: 82725 次
社区版块
存档分类
最新评论

一点小小的细节

阅读更多

今天看到一本书上写的,有关定义实体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签名一样的.

好了,睡觉去了

分享到:
评论

相关推荐

    安装小小输入法时出现can't read yong.xml错误

    在Linux系统中,安装小小输入法时...理解这一点对于避免类似问题至关重要,尤其是对于Linux初学者和那些容易忽略细节的高级用户。通过细致的排查和分析,我们可以克服这个安装障碍,享受小小输入法带来的便捷输入体验。

    一点小小的创意css鼠标放上去则显示电话号码

    ### 一点小小的创意CSS:鼠标放上去则显示电话号码 #### 概述 本文将详细介绍一个有趣且实用的CSS技巧——当鼠标悬停在特定元素上时,显示隐藏的电话号码。这一效果不仅可以用于个人博客或网站增添互动性,还可以...

    青春励志演讲稿:注重细节-促成功之路.docx

    同时,美国“挑战者号”航天飞机的悲剧,也是由于一个小小的螺丝钉未拧紧,凸显了细节对安全的巨大影响。 演讲稿提醒我们,现代社会的快速节奏和专业化分工使得细节更加重要。生活是由无数细节组成的,没有细节就...

    小小班教师个人总结.doc

    这些看似微不足道的细节,对于小小班的孩子来说却是建立良好生活习惯的重要步骤。 在教育教学方面,我充分认识到这个年龄段的孩子具有极强的模仿能力和探索欲望。因此,我采用寓教于乐的方式,将知识融入到生动有趣...

    安全,怎能说说而已.docx

    这个故事告诉我们,在安全问题上,任何一点小小的忽视都可能导致不可挽回的损失。 #### 五、从我做起,关注细节 在日常工作中,我们经常强调“主人翁意识”。而在安全问题上,这种意识尤为重要。每一次事故的发生...

    MooBox 基于Mootools的对话框插件

    一方面出于对mootools的兴趣(虽然没有jQuery那么hot), 另一方面,也是想为mootools的推广添一点小小的力量,虽然这微不足道. 加上前面发布过的2个mootools组件, 写下来总的感觉是: 在效果实现方面, 确实用jQuery编写要...

    超越LOGO设计

    一点小小的调整可能会对整体设计产生巨大的影响。 3. 沟通能力:设计师不仅仅是创造者,同时也是沟通者。他们需要与客户沟通,确保理解客户需求,并且能够清楚地表达设计理念。 4. 创新思维:优秀的设计师能够以...

    PCB品质管理:100-1=0.pdf

    首先,文档提出“100-1=0”的概念,这是对质量控制中零缺陷理念的强调,意味着在生产过程中任何一点小小的疏忽或差错都可能造成100%的产品报废。这要求所有员工都要有高度的质量意识,认识到品质是企业的生命线,是...

    小学生消防安全知识演讲稿5篇.docx

    火灾往往是由一点小小的火星,一缕不起眼的烟头,一个随意放置的易燃物品所引发的。这些细节,看似微不足道,但我们必须重视,因为生命无法重来。当我们看到那一幕幕因为火灾而消逝的生命,我们是否应该警醒?安全无...

    初中语文文摘文苑卖菜

    在IT创新中,一点小小的改变,比如一项新技术的应用,可能就会开启全新的可能性。 8. **品质至上**:卖菜的故事中,品质决定了销售,同样,在IT产品中,质量是核心竞争力,只有高质量的产品才能赢得用户的信赖和...

    初中语文文摘情感筷子上的修为

    父亲的话“不要小瞧一双筷子,一个小小的细节,可以看出拿筷子者的修为和人品”,提醒我们要注意日常生活中的点滴行为,因为这些细微之处往往最能反映一个人的真实品性。一个人对待小事的态度,往往决定了他处理大事...

    实习第二周个人总结.doc

    一次小小的疏忽就可能导致严重的问题,因此,保持专注,一丝不苟地完成每一项任务是至关重要的。 其次,保持平和的处事态度是维持高效工作的关键。在日复一日的重复工作中,可能会让人产生厌倦感,甚至导致工作效率...

    消防征文10篇.docx

    2. **防火从细节做起**:文章举例说明,一点小小的疏忽,如随手丢弃的烟蒂,都可能导致一场大火,提醒人们在日常生活中要注意消防安全,如妥善处理火源,避免在禁止烟火的地方使用明火。 3. **消防工作的重要性**:...

    聊聊网页中的“微创新”

    老乔布斯说过,所谓的微创新,并不是让你把过去的东西都推翻掉,而是在过去的基础上去不断的调整和变化,甚至可能只是一点小小的功能,但是最后给客户带来的体验是非常棒的,这就是微创新。对一个成功的产品来说,再...

    数字电压表说明书.pdf

    数字电压表测试结果表明尽管存在一点小小的误差,但基本达到了任务书中要求的效果,具有一定的实用价值。 八、结论 数字电压表设计基于AT89S52单片机和ADC0809模数转换芯片的应用,具有很高的实用价值和应用前景。...

    11商务礼仪1109版.pptx

    【商务礼仪】是现代社会中,特别是商业环境中,个人素质与企业形象的重要体现。学习和遵守商务礼仪对于个人和企业的成功至关重要...在商务交往中,一个小小的细节可能就决定了成败,因此,重视和掌握商务礼仪至关重要。

    铁路安全生产月演讲:多点细心、多点静思.docx

    就像我在4月1日的工作中犯下的错误,一个小小的疏忽,一个不起眼的细节——在调车计划中忽略了禁止溜放的标识,给作业带来了困扰。这个错误看似微不足道,实则揭示了一个深刻的道理:任何一次的马虎大意,都可能酿成...

    环境革命动员大会发言稿.docx

    真正改善环境需要我们每个人的日常参与,从不乱丢垃圾、节约用水用电,到选择环保产品,每一点小小的改变都有深远的影响。正如我们所知,中国的环保问题不容乐观,全球50%以上的文明提示语使用中文,这反映出我们在...

    核舟记课下注释.doc

    1. "奇巧人"是指具有独特技巧和创新精神的工匠,文中特指王叔远,他在小小的核桃上创造出令人惊叹的艺术品。 2. "径寸之木",径指的是直径,文中指长度不足一寸的微小木材,王叔远却能在如此微小的空间里进行雕刻。 ...

Global site tag (gtag.js) - Google Analytics