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

一点小小的细节

阅读更多

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

好了,睡觉去了

分享到:
评论

相关推荐

    MooBox 基于Mootools的对话框插件

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

    聊聊网页中的“微创新”

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

    JAVA自学之路

    如果一个问题牵扯的面比较广,就干脆到网上搜索一些相关的专题,比如“java 乱码 mysql” “oracle 创建用户”等等,如果有必要,不要犯懒,勤动手写一些小小的测试程序,来弄明白知识点的细节。这也是涨知识的重要...

    面向对象技术课程设计

    这就需要在商品product中加入Observer这样角色,以便product细节发生变化时,Observer能自动观察到这种变化,并能进行及时的update动作. &lt;br&gt;2、适配器模式 &lt;br&gt; 小小最爱喝饮料,可妈妈不让,每天都用保温杯给她...

    算法基础.打开算法之门.[美]托马斯 H.科尔曼(带详细书签)

    小小的GPS是如何只在几秒钟内就从无数条可能路径中找出到达目的地的最快捷路径的呢?在网上购物时,又如何防止他人窃取你的信用卡账号呢?解决这些问题,以及大量其他问题的答案均是算法。我写本书的目的就是为你...

    自己动手写操作系统(含源代码).part2

    书中讲解了大量在开发操作系统中需注意的细节问题,这些细节不仅能使读者更深刻地认识操作系统的核心原理,而且使整个开发过程少走弯路。本书分上下两篇,共11章。其中每一章都以前一章的工作成果为基础,实现一项新...

    自己动手写操作系统(含源代码).part1

    书中讲解了大量在开发操作系统中需注意的细节问题,这些细节不仅能使读者更深刻地认识操作系统的核心原理,而且使整个开发过程少走弯路。本书分上下两篇,共11章。其中每一章都以前一章的工作成果为基础,实现一项新...

    CHKen Tray Clock

     还有更多小小的细节....  移除自动安装功能 2009.04.21 - 2.0  增强定时器功能  定时关机功能  周期功能  自定义名称  过期在下次启动时会提示了  添加任务栏支持显示CPU和网络  定时器和系统设置分开 ...

    Accelerated C++ PDF 中英文版

    本书这一版本充分体现了这一点。第4版的改动为了体现现代C++编程风格,我们重新组织并重写了本书。书中不再强调低层编程技术,而把中心转向标准库的使用。书中很早就开始介绍标准库,示例也已经重新改写,充分利用了...

    Accelerated C++源代码

    本书这一版本充分体现了这一点。第4版的改动为了体现现代C++编程风格,我们重新组织并重写了本书。书中不再强调低层编程技术,而把中心转向标准库的使用。书中很早就开始介绍标准库,示例也已经重新改写,充分利用了...

    git-meta:git-meta项目的存储库-使用Git子模块构建自己的monorepo

    多一点细节 Git-meta既描述了体系结构,又提供了一组工具来促进单仓库和相关工作流程的实施。 除了能够安装此存储库中提供的工具之外,git-meta仅需要Git。 Git-meta不与任何特定的Git托管解决方案绑定,并

    旅游幻灯片之:美丽欧洲PPT.rar

    你如果喜欢可以在这道炖菜里加上各种时蔬,比如胡萝卜,白萝卜,蘑菇等等,有时候一点小小的创意,会给你的菜式带来意想不到的效果。这道爱尔兰炖牛肉的小秘密就是啤酒的使用,传统的做法是用爱尔兰的黑啤来炖煮牛肉...

Global site tag (gtag.js) - Google Analytics