学生和老师就是多对多的关系。一个学生有多个老师,一个老师教多个学生。
学生和档案就是一对一的关系(不知道国外的学生有没有档案?)。
为了实现多对多的关系,数据库中需要关联表,用以在两个实体间建立关联。JBoss可以自动生成关联表,你也可以@AssociationTable来指定关联表的信息。
如:
@ManyToMany(cascade = {CascadeType.CREATE, CascadeType.MERGE}, fetch = FetchType.EAGER, isInverse = true)
@AssociationTable(table = @Table(name = "STUDENT_TEACHER"),
joinColumns = {@JoinColumn(name = "TEACHER_ID")},inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID")})
@ AssociationTable的注释声明如下:
@Target({METHOD, FIELD})
public @interface AssociationTable {
Table table() default @Table(specified=false);
JoinColumn[] joinColumns() default {};
JoinColumn[] inverseJoinColumns() default {};
}
关联表注释指定了关联表的名称、主表的列和从表的列。
为了实现一对一的关系,需要用@OneToOne来注释。
如:
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "DOSSIER_ID")
public Dossier getDossier()
{
return dossier;
}
这定义了一个单向的一对一的关系。如果在Dossier也定义了相关的关联,那么它就是双向的。双向的意思就是通过一个Student实体就可以查找到一个Dossier,通过一个Dossier就可以查找到一个Student。
@ OneToOne的注释声明如下:
@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface OneToOne {
String targetEntity() default "";
CascadeType[] cascade() default {};
FetchType fetch() default EAGER;
boolean optional() default true;
}
这个例子主要有以下几个文件,这个例子主要实现了学生和老师、学生和档案之间的关系。Student、Teacher、Dossier都是实体Bean。Student和Dossier是一个双向的OneToOne之间的关系,Student和Teacher是ManyToMany的关系,也是双向的。和前面的例子一样,我们还是使用Client测试。
Student.java:实体Bean。
Dossier.java:实体Bean所依赖的类。
Teacher.java:实体Bean所依赖的类。
EntityTest.java:会话Bean的业务接口
EntityTest Bean.java:会话Bean的实现类
Client.java:测试EJB的客户端类。
jndi.properties:jndi属性文件,提供访问jdni的基本配置属性。
Build.xml:ant 配置文件,用以编译、发布、测试、清除EJB。
下面针对每个文件的内容做一个介绍。
Student.java
package com.kuaff.ejb3.relationships;
import javax.ejb.CascadeType;
import javax.ejb.Entity;
import javax.ejb.FetchType;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.JoinColumn;
import javax.ejb.OneToOne;
import javax.ejb.ManyToMany;
import javax.ejb.Table;
import javax.ejb.AssociationTable;
import java.util.ArrayList;
import java.util.Set;
import java.util.Collection;
import java.io.Serializable;
@Entity
@Table(name = "STUDENT")
public class Student implements Serializable
{
private int id;
private String first;
private String last;
private Dossier dossier;
private Set<Teacher> teachers;
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public void setFirst(String first)
{
this.first = first;
}
public String getFirst()
{
return first;
}
public void setLast(String last)
{
this.last = last;
}
public String getLast()
{
return last;
}
public void setDossier(Dossier dossier)
{
this.dossier = dossier;
}
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "DOSSIER_ID")
public Dossier getDossier()
{
return dossier;
}
public void setTeacher(Set<Teacher> teachers)
{
this.teachers = teachers;
}
@ManyToMany(cascade = {CascadeType.CREATE, CascadeType.MERGE}, fetch = FetchType.EAGER, isInverse = true)
@AssociationTable(table = @Table(name = "STUDENT_TEACHER"),
joinColumns = {@JoinColumn(name = "TEACHER_ID")},inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID")})
public Set<Teacher> getTeacher()
{
return teachers;
}
}
Dossier.java
package com.kuaff.ejb3.relationships;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
@Entity
public class Dossier implements java.io.Serializable
{
private Long id;
private String resume;
@Id(generate = GeneratorType.AUTO)
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public void setResume(String resume)
{
this.resume = resume;
}
public String getResume()
{
return resume;
}
}
Teacher.java
package com.kuaff.ejb3.relationships;
import javax.ejb.AssociationTable;
import javax.ejb.Basic;
import javax.ejb.CascadeType;
import javax.ejb.Column;
import javax.ejb.Entity;
import javax.ejb.FetchType;
import javax.ejb.Id;
import javax.ejb.JoinColumn;
import javax.ejb.ManyToMany;
import javax.ejb.Table;
import javax.ejb.Transient;
import javax.ejb.Version;
import java.util.Set;
import javax.ejb.GeneratorType;
@Entity
public class Teacher implements java.io.Serializable
{
private Long id;
private String resume;
private String name;
private String info;
private Set<Student> students;
@Id(generate = GeneratorType.IDENTITY)
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setInfo(String info)
{
this.info = info;
}
public String getInfo()
{
return info;
}
public void setStudents(Set<Student> students)
{
this.students = students;
}
@ManyToMany(cascade = {CascadeType.CREATE, CascadeType.MERGE}, fetch = FetchType.EAGER)
@AssociationTable(table = @Table(name = "STUDENT_TEACHER"),
joinColumns = {@JoinColumn(name = "TEACHER_ID",referencedColumnName="ID")},
inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID",referencedColumnName="ID")})
public Set<Student> getStudents()
{
return students;
}
}
EntityTest.java
package com.kuaff.ejb3.relationships;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface EntityTest
{
public void createData();
public List findByName(String name);
}
EntityTestBean.java
package com.kuaff.ejb3.relationships;
import javax.ejb.EntityManager;
import javax.ejb.Inject;
import javax.ejb.Stateless;
import java.util.HashSet;
import java.util.Set;
import java.util.