EJB学习日记(9) EJBBean生活
程序员文章站
2022-06-06 14:59:44
...
实体BEAN的七种关系之---------一对一双向
One-to-One Bidirectional Relationship
一对一双向的情况其实在现实生活中也是挺常见的,最常见的就是人和身份证的关系了,我们每个人都有一个身份证(只要你年满16岁,并且申请了),并且每个身份证都对应一个人,我们可以根据一个人查到他的身份证号码,也可以根据一个身份证号码查到一个人的具体情况,(第一代身份证重码的情况还是有的:( ) .好了,闲话不多说,还是看看代码吧.
先定义一个Person类
/*
* Person.java
*
* Created on 2007-9-15, 0:11:58
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author Admin
*/
@Entity
public class Person implements Serializable {
private IDCard idCard;
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private int age;
private Address address;
@OneToOne(cascade=CascadeType.ALL,optional=true)
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@OneToOne(cascade={CascadeType.ALL},optional=true)
public IDCard getIdCard() {
return idCard;
}
public void setIdCard(IDCard iDCard) {
this.idCard = iDCard;
}
}
* Person.java
*
* Created on 2007-9-15, 0:11:58
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author Admin
*/
@Entity
public class Person implements Serializable {
private IDCard idCard;
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private int age;
private Address address;
@OneToOne(cascade=CascadeType.ALL,optional=true)
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@OneToOne(cascade={CascadeType.ALL},optional=true)
public IDCard getIdCard() {
return idCard;
}
public void setIdCard(IDCard iDCard) {
this.idCard = iDCard;
}
}
IDCard类
/*
* IDCard.java
*
* Created on 2007-9-18, 17:05:51
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
*
* @author hadeslee
*/
@Entity
public class IDCard implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Person person;
private String cardNumber;
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
@OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
* IDCard.java
*
* Created on 2007-9-18, 17:05:51
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lbf.entitybean.test1;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
*
* @author hadeslee
*/
@Entity
public class IDCard implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Person person;
private String cardNumber;
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
@OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
在这里我们可以看到一对一单向和一对一双向其实差别也不是很大,无非就是在对方的类里面也定义了一个自己,这样就互相都能找到对方了,在数据表的表现来看,和一对一单向是一模一样的,都是从Person表里面起一个外键连到IDCard的主键.但是只有这种方法吗?
在Person类里面,定义的IDCard注释是和Address一样的,可是我们在IDCard里面定义的注释却和Person里面的不一样:
@OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false)
public Person getPerson() {
return person;
}
多了一个mappedBy这个方法,它表示什么呢?它表示此表和Person的关系是定义在Person里面的idCard这个成员上面的,它表示此表是一对一关系中的从表,也就是关系是在person表里面维护的,这一点很重要.
我们也可以让主导权在IDCard上面,也就是让它产生一个指向Person的外键,这也是可以的,但是最好是让 Person来维护整个关系,这样更符合我们正常的思维.
我们也可以看到在Person里面IDCard是注释是optional=true,也就是说一个人是可以没有身份证的,但是一个身份证不可以没有人,所以在IDCard里面注释person的时候,optional就为false了,这样就可以防止一个空的身份证记录进数据库.
上一篇: js替换所要替换的所有字符