|
學生和老師就是多對多的關系。一個學生有多個老師,一個老師教多個學生。
學生和檔案就是一對一的關系(不知道國外的學生有沒有檔案?)。
為了實現多對多的關系,數據庫中需要關聯表,用以在兩個實體間建立關聯。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
@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
{
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
{
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
@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
{
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
{
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
Set
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的方法,并提供了查找老師的方法。
Client.Java
package com.kuaff.ejb3.secondary;
import Javax.naming.InitialContext;
import Javax.naming.NamingException;
import Java.util.List;
public class Client
{
public static void main(String[] args) throws NamingException
{
InitialContext ctx = new InitialContext();
StudentDAO dao = (StudentDAO) ctx.lookup(StudentDAO.class.getName());
int id = dao.create("晁","岳攀","8","smallnest@kuaff.com","男");
dao.create("朱","立煥","6","zhuzhu@kuaff.com","女");
List list = dao.findAll();
for(Object o:list)
{
Student s = (Student)o;
System.out.printf("%s%s的性別:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getGender());
dao.evict(s);
}
}
}
這個客戶端用來測試。
請運行{$JBOSS_HOME}/bin目錄下的run.bat: run 主站蜘蛛池模板: 天天干天天干天天干天天 | 男人把女人c爽的免费视频 男人操女人免费视频 | 国产中文在线视频 | 成人午夜在线播放 | 99精品在免费线视频 | 亚洲春黄在线观看 | 国产精品极品美女自在线观看免费 | 国产亚洲漂亮白嫩美女在线 | 韩国一级毛片视频免费观看 | 久久精品国产99国产精品 | 中文字幕 亚洲精品 第1页 | 日本老年人精品久久中文字幕 | 欧美岛国 | 日本久久综合网 | 91精品视频免费观看 | 欧美成人高清手机在线视频 | 国产精品特黄一级国产大片 | 久久综合九色综合欧洲色 | 亚洲国产一区二区三区 | 亚洲一二区视频 | 狠狠干.com| 亚州一级毛片 | 怡红院免费全部视频在线视频 | 日韩久久网 | 麻豆精品在线视频 | 国产精品久久久亚洲第一牛牛 | 中文字幕一区二区三区四区 | 国产午夜视频在线 | 色天使色婷婷在线影院亚洲 | 麻豆专区| 成人精品视频在线观看播放 | 亚洲国产成人在线观看 | 国产69久久精品成人看 | 婷婷99精品国产97久久综合 | 中文字幕日本久久2019 | 国产精品欧美在线不卡 | 综合涩| 美国一级做a爰片性色毛片 美国一区二区三区 | 深爱五月婷婷 | gogo全球高清人体成人 | 欧洲在线观看在线视频吗 |