以前看到一本书上写的,有关定义实体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签名一样的.
分享到:
相关推荐
EJB初学者的列子,JBOSS包等的下载
EJB初学教程分章节示例,分几个层次学习,适合初学者学习实验
EJB初学者常有的十一个疑惑 EJB与JAVA BEAN的区别? 答:Java Bean 是可复用的组件,对Java Bean并没有严格的规范,理论上讲,任何一个Java类都可以是一个Bean。但通常情况下,由于Java Bean是被容器所创建(如...
ejb3.0教材,适合ejb初学者阅读!
本源代码主要向EJB初学者介绍JMS的使用,如何创建和初始化一个请求Requestor对象、如何初始化请求Requestor对象、如何建立一个会话和发送请求、如何打印出请求的过程信息、如何接受由反馈者replier发回的消息等。
ejb的教程,挺不错的,大家不妨看看,保证有收获
第1篇对EJB编程基础进行介绍,它概要性地对EJB进行了阐述,无论是EJB初学者,还是资深EJB专家,这部分内容都值得阅读;第2篇重点关注EJB编程的具体内容和过程,其中,研究了如何开发如下三种EJB组件:会话Bean、实体Bean和...
本教程适合具有Java 语言基础的EJB初学者。随着EJB3的发展,EJB2 将会逐步成为历史!!!!!1
本教程适合具有Java 语言基础的EJB 初学者。有读者来邮件问需不需要先学EJB2.0,作者明确地告诉你不用 学了。随着EJB3 的发展,EJB2 将会逐步成为历史。
实战EJB 实战EJB 实战EJB 电子书 pdf
最新EJB学习教程,适合初学者学习掌握,讲解比较精辟,
本书内容翔实、深入浅出,提供了详细的讨论和实例,对于懂Java语言的EJB初学者是一本有益的指导书。本书所附光盘包括:WebLogic Server 6.1的试用版、WebGain's VisualCafe 4.5的试用版、 WebGain's TopLink 3.5.1的...
EJB方面EJB方面EJB方面EJB方面EJB方面
EJB初学者的10大误区,学习EJB必知道的。EJB3.0的特性等等。喜欢的朋友下下看看。
ejb开发技术使用手册,初学者非常适合,内容详细,有案例,有重点,使用步骤明确!为ejb开发做了详细的步骤总结!
EJB集群资料EJB集群资料EJB集群资料EJB集群资料
EJB3课堂笔记,通俗易懂,很简洁,PDF格式
一个简单的Stateless Session Bean的例子,里面主要实现一个与时间有关的... 同时编写了一个客户端测试程序,展现的是一个简单的Stateless Session Bean的实现过程,希望能够对复杂的EJB的开发起一些抛砖引玉的作用。
期待已久的EJB3.0最终规范已经发布了。虽然EJB3.0 最终规范出来...本教程适合具有Java 语言基础的EJB初学者。有读者来邮件问需不需要先学EJB2.x,作者明确地告诉你不用学了。随着EJB3的发展,EJB2.x将会逐步成为历史。
本书面向EJB 3.0的实际应用开发,通过大量的实例,循序渐进地为读者介绍了有关EJB 3.0开发所涉及的...本书非常适合EJB 3.0初学者和从事EJB项目开发的人员使用,对于开发大型的企业分布式程序是一本非常好的参考书。