EJB学习日记(8) EJBBeanJBoss生活
程序员文章站
2022-06-06 15:00:38
...
实体BEAN的七种关系之---------一对一单向
一对一单向,顾名思义,就是只要求从A方到达B方,而不需要从B方到达A方,典型的例子就是,一个人对应一个地址,因为现实生活中,一个地址可能住很多人,所以一般我们只需要根据人查到它的地址,而不太会需要从一个地址去查谁住在那里,不过,真的有这种需求的话,我们就要以用另外一种关系来实现了,这个以后再讲
首先我们声明一个人的实体类,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 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;
}
}
* 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 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;
}
}
再来看看Address的实体BEAN定义
/*
* Address.java
*
* Created on 2007-9-15, 0:13:50
*
* 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.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author Admin
*/
@Entity
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String country,province,city,street;
private int postcode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getPostcode() {
return postcode;
}
public void setPostcode(int postcode) {
this.postcode = postcode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
* Address.java
*
* Created on 2007-9-15, 0:13:50
*
* 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.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author Admin
*/
@Entity
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String country,province,city,street;
private int postcode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getPostcode() {
return postcode;
}
public void setPostcode(int postcode) {
this.postcode = postcode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
}
以上便是两个类的定义,从Person里面我们可以看到如下的代码
@OneToOne(cascade=CascadeType.ALL,optional=true)
public Address getAddress() {
return address;
}
在这里的@OneToOne就是注释是一对一的关系,其中cascade是表示级联的关系,是级联删除还是级联更新,还是所有,我们这里选择的是所有,这样的好处就是,我们在插入Person的时候,就顺带的把Person里面包含的Address一起插入数据库里面,当我们删除Person的时候,也是把这个Person对应的Address从数据库里面删除,否则就需要我们人工的删除两遍或者插入两遍等等,optional表示这个成员是不是可选的,我们这里是可选的,也就是说一个人可以没有地址(比如流浪汉:)).
然后我们发现,在Address里面只有一些简单的EntityBean的注释,并没有表示关系的注释,这是因为本例是一对一单向的实现,Person里面有 Address,而Address却什么都不知道,它对应到数据库里面只是一张简单的表而已,Person对应到数据库里面就有一个指向Address的外键.我们也可以增加注释指定外键的列的名字,如下:
@OneToOne(cascade=CascadeType.ALL,optional=true)
@JoinColumn(name="addressID")
public Address getAddress() {
return address;
}
如果我们不加的话,也是可以通过的,在JBOSS里面,它会自动帮你生成你指向这个类的类名加上下划线再加上id的列,也就是默认列名是:address_id.
如果是主键相关联的话,那么可以运用如下注释
@OneToOne(cascade={CascadeType.ALL})
@PrimaryKeyJoinColumn
public Address getAddress( ) {
return homeAddress;
}
它表示两张表的关联是根据两张表的主键的
以下是一些注释的定义,我们看一下可以了解一下这些注释有哪些方法,
public @interface JoinColumn
{
String name( ) default "";
String referencedColumnName( ) default "";
boolean unique( ) default false;
boolean nullable( ) default true;
boolean insertable( ) default true;
boolean updatable( ) default true;
String columnDefinition( ) default "";
String table( ) default "";
}
{
String name( ) default "";
String referencedColumnName( ) default "";
boolean unique( ) default false;
boolean nullable( ) default true;
boolean insertable( ) default true;
boolean updatable( ) default true;
String columnDefinition( ) default "";
String table( ) default "";
}
public @interface OneToOne
{
Class targetEntity( ) default void.class;
CascadeType[] cascade( ) default {};
FetchType
fetch( ) default EAGER;
boolean optional( ) default true;
String mappedBy( ) default "";
}
{
Class targetEntity( ) default void.class;
CascadeType[] cascade( ) default {};
FetchType
fetch( ) default EAGER;
boolean optional( ) default true;
String mappedBy( ) default "";
}
public @interface PrimaryKeyJoinColumn
{
String name( ) default "";
String referencedColumnName( ) default "";
String columnDefinition( ) default "";
}
{
String name( ) default "";
String referencedColumnName( ) default "";
String columnDefinition( ) default "";
}
好了,一对一单向差不多就这么些吧,明天再看一下一对一双向的情况.
上一篇: 上传文件ajax效果 Ajax