學生和老師就是多對多的關系。一個學生有多個老師,一個老師教多個學生。
學生和檔案就是一對一的關系(不知道國外的學生有沒有檔案?)。
為了實現多對多的關系,數據庫中需要關聯表,用以在兩個實體間建立關聯。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
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 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 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 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 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 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.List;
@Stateless
public class EntityTestBean implements EntityTest
{
private @Inject EntityManager manager;
public void createData()
{
Teacher teacher1 = new Teacher();
Teacher teacher2 = new Teacher();
Set students1 = new HashSet();
Set students2 = new HashSet();
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
Dossier dossier1 = new DOSsIEr();
Dossier dossier2 = new DOSsIEr();
Dossier dossier3 = new DOSsIEr();
teacher1.setId(new Long(1));
teacher1.setName("hushisheng");
teacher1.setInfo("胡時勝教授,博士生導師");
manager.create(teacher1);
teacher2.setId(new Long(2));
teacher2.setName("liyongchi");
teacher2.setInfo("李永池教授,博士生導師");
manager.create(teacher2);
student1.setFirst("晁");
student1.setLast("岳攀");
DOSsIEr1.setResume("這是晁岳攀的檔案");
student1.setDossier(DOSsIEr1);
students1.add(student1);
student2.setFirst("趙");
student2.setLast("志偉");
DOSsIEr2.setResume("這是趙志偉的檔案");
student2.setDossier(DOSsIEr2);
students1.add(student2);
student3.setFirst("田");
student3.setLast("明");
DOSsIEr3.setResume("這是田明的檔案");
student3.setDossier(DOSsIEr3);
students2.add(student3);
teacher1.setStudents(students1);
teacher2.setStudents(students2);
}
public List findByName(String name)
{
return manager.createQuery("from Teacher t where t.name = :name").setParameter("name", name).listResults();
}
}
在這個會話Bean中提供了創建各個實體Bean的方法,並提供了查找老師的方法。