一、搭建环境
1) 搭建数据库环境 1、安装数据库(MySQL 5.0.24), 用户: root,密码: (空) 。
2、建库piscesdb。
3、建表address:
4、建表userinfo:
2) 获取额外的jar包 下载JPA的实现类, 去[url=https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html]https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html[/url]下载GlassFish v1 FCS branch版本,
进行安装后得到toplink-essentials.jar,toplink-essentials-agent.jar 两个包,将这两个包和mysql的驱动包加入到项目的classpath路径中去。
3) 开发环境 JDK: jdk 6.0 beta2 (JDK 5.0 也可以)
IDE: NetBeans 5.0 中文版
二、开发1) 创建实体Entity类 1、地址类 Address :
1) 搭建数据库环境 1、安装数据库(MySQL 5.0.24), 用户: root,密码: (空) 。
2、建库piscesdb。
3、建表address:
drop table address;
create table address (
addressID int not null,
city varchar(55) not null,
street varchar(55) NOT NULL,
zip varchar(8) NOT NULL,
PRIMARY KEY (addressID)
);
insert into address values (1, "深圳", "坂田市场", "518001");
insert into address values (2, "深圳", "坂田路口", "518002");
insert into address values (3, "深圳", "四季花城", "518003");
4、建表userinfo:
droptable userinfo;
createtable userinfo (
userID intnotnull, /** 用户id */
username varchar(20) notnull, /** 姓名 */
birthday datetimenull, /** 出生日期 */
sex varchar(8) notnull, /** 性别 */
addressID intnotnull, /** 地址id */
PRIMARYKEY (userID)
);
insertinto userinfo values (1, "张金雄", null, "male", 1);
insertinto userinfo values (2, "李某某", null, "male", 2);
insertinto userinfo values (3, "王某某", '2006-08-10', "female", 3);
insertinto userinfo values (4, "陈某某", '2006-08-12', "male", 3);
2) 获取额外的jar包 下载JPA的实现类, 去[url=https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html]https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html[/url]下载GlassFish v1 FCS branch版本,
进行安装后得到toplink-essentials.jar,toplink-essentials-agent.jar 两个包,将这两个包和mysql的驱动包加入到项目的classpath路径中去。
3) 开发环境 JDK: jdk 6.0 beta2 (JDK 5.0 也可以)
IDE: NetBeans 5.0 中文版
二、开发1) 创建实体Entity类 1、地址类 Address :
/*
* Address.java
*/
package org.pisces.persist;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
*
* @author kamhung
*/
@Entity
publicclass Address implements Serializable {
//地址id, 不能为空, 必须唯一
@Id
@Column(name ="addressid", unique=true, nullable=false)
privateint addressid;
//城市, 不能为空
@Column(name ="city", nullable=false)
private String city;
//街道, 不能为空
@Column(name ="street", nullable=false)
private String street;
//邮政编码, 不能为空
@Column(name ="zip", nullable=false)
private String zip;
public Address() {
}
public Address(int addressid) {
this.setAddressid(addressid);
}
publicint getAddressid() {
returnthis.addressid;
}
publicvoid setAddressid(int addressid) {
this.addressid = addressid;
}
public String getCity() {
returnthis.city;
}
publicvoid setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
publicvoid setStreet(String street) {
this.street = street;
}
public String getZip() {
returnthis.zip;
}
publicvoid setZip(String zip) {
this.zip = zip;
}
@Override
publicint hashCode() {
returnthis.addressid;
}
@Override
publicboolean equals(Object object) {
if (!(object instanceof Address)) returnfalse;
final Address other = (Address)object;
returnthis.addressid == other.addressid;
}
@Override
public String toString() {
return"Address[addressid="+ getAddressid() +", city='"+ getCity() +"', street='"+ getStreet() +"', zip='"+ getZip() +"";
}
}
2、用户类 UserInfo:
/*
* UserInfo2.java
*/
package org.pisces.persist;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
importstatic javax.persistence.CascadeType.*;
/**
*
* @author kamhung
*/
@Entity
publicclass UserInfo implements Serializable {
//用户id, 不能为空, 必须唯一
@Id
@Column(name ="userid", unique=true, nullable=false)
privateint userid;
//用户名, 不能为空
@Column(name ="userName", nullable=false)
private String userName;
//性别, 不能为空
@Column(name ="sex", nullable=false)
private String sex;
//出生日期, 可以为空
@Column(name ="birthday")
private Timestamp birthday;
//地址, 不能为空
//PERSIST 表示更新、新增UserInfo数据时会同时更新、新增Address的数据
//REMOVE 表示从数据库删除UserInfo会同时删除Address表中对应的数据
@OneToOne(cascade={PERSIST, REMOVE})
@JoinColumn(name ="addressID", nullable=false)
private Address address;
public UserInfo() {
}
public UserInfo(int userid) {
this.setUserid(userid);
}
@Override
publicint hashCode() {
returnthis.getUserid();
}
@Override
publicboolean equals(Object object) {
if (!(object instanceof UserInfo)) returnfalse;
final UserInfo other = (UserInfo)object;
returnthis.userid == other.userid;
}
@Override
public String toString() {
return"UserInfo[userid="+this.userid +", userName='"+ userName +"', sex='"+ sex
+"', birthday="+ birthday +", address="+ address +"";
}
publicint getUserid() {
return userid;
}
publicvoid setUserid(int userid) {
this.userid = userid;
}
public String getUserName() {
return userName;
}
publicvoid setUserName(String userName) {
this.userName = userName;
}
public Timestamp getBirthday() {
return birthday;
}
publicvoid setBirthday(Timestamp birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
public Address getAddress() {
return address;
}
publicvoid setAddress(Address address) {
this.address = address;
}
}