|
學(xué)生和老師就是多對(duì)多的關(guān)系。一個(gè)學(xué)生有多個(gè)老師,一個(gè)老師教多個(gè)學(xué)生。
學(xué)生和檔案就是一對(duì)一的關(guān)系(不知道國(guó)外的學(xué)生有沒(méi)有檔案?)。
為了實(shí)現(xiàn)多對(duì)多的關(guān)系,數(shù)據(jù)庫(kù)中需要關(guān)聯(lián)表,用以在兩個(gè)實(shí)體間建立關(guān)聯(lián)。JBoss可以自動(dòng)生成關(guān)聯(lián)表,你也可以@AssociationTable來(lái)指定關(guān)聯(lián)表的信息。
如:
@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 {};
}
關(guān)聯(lián)表注釋指定了關(guān)聯(lián)表的名稱(chēng)、主表的列和從表的列。
為了實(shí)現(xiàn)一對(duì)一的關(guān)系,需要用@ONEToOne來(lái)注釋。
如:
@ONEToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "DOSSIER_ID")
public Dossier getDossier()
{
return dossier;
}
這定義了一個(gè)單向的一對(duì)一的關(guān)系。如果在Dossier也定義了相關(guān)的關(guān)聯(lián),那么它就是雙向的。雙向的意思就是通過(guò)一個(gè)Student實(shí)體就可以查找到一個(gè)Dossier,通過(guò)一個(gè)Dossier就可以查找到一個(gè)Student。
@ ONEToOne的注釋聲明如下:
@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface ONEToOne {
String targetEntity() default "";
CascadeType[] cascade() default {};
FetchType fetch() default EAGER;
boolean optional() default true;
}
這個(gè)例子主要有以下幾個(gè)文件,這個(gè)例子主要實(shí)現(xiàn)了學(xué)生和老師、學(xué)生和檔案之間的關(guān)系。Student、Teacher、Dossier都是實(shí)體Bean。Student和Dossier是一個(gè)雙向的ONEToOne之間的關(guān)系,Student和Teacher是ManyToMany的關(guān)系,也是雙向的。和前面的例子一樣,我們還是使用Client測(cè)試。
Student.Java:實(shí)體Bean。
Dossier.Java:實(shí)體Bean所依賴(lài)的類(lèi)。
Teacher.Java:實(shí)體Bean所依賴(lài)的類(lèi)。
EntityTest.Java:會(huì)話Bean的業(yè)務(wù)接口
EntityTest Bean.Java:會(huì)話Bean的實(shí)現(xiàn)類(lèi)
Client.Java:測(cè)試EJB的客戶端類(lèi)。
jndi.properties:jndi屬性文件,提供訪問(wèn)jdni的基本配置屬性。
Build.xml:ant 配置文件,用以編譯、發(fā)布、測(cè)試、清除EJB。
下面針對(duì)每個(gè)文件的內(nèi)容做一個(gè)介紹。
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("胡時(shí)勝教授,博士生導(dǎo)師");
manager.create(teacher1);
teacher2.setId(new Long(2));
teacher2.setName("liyongchi");
teacher2.setInfo("李永池教授,博士生導(dǎo)師");
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();
}
}
在這個(gè)會(huì)話Bean中提供了創(chuàng)建各個(gè)實(shí)體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);
}
}
}
這個(gè)客戶端用來(lái)測(cè)試。
請(qǐng)運(yùn)行{$JBOSS_HOME}/bin目錄下的run.bat: run 主站蜘蛛池模板: 天天做天天爱天天爽天天综合 | 男女一区二区三区免费 | 国产精品久久久久免费a∨ 国产精品久久久久免费视频 | 日本乱理伦中文三区 | 97大香伊在人人线色 | 青青国产成人久久激情91麻豆 | 日韩一区二区免费看 | 午夜国产在线观看 | 中国a毛片 | 国产日韩在线 | 国产成人免费视频 | 国产福利最新手机在线观看 | 91在线视频播放 | 精品日韩欧美国产一区二区 | 国产精品区网红主播在线观看 | 欧美一区二区三区视视频 | 亚洲欧洲国产经精品香蕉网 | 视频一区中文字幕 | 久久精品99无色码中文字幕 | 国产激情在线视频 | 久9精品视频 | 久久r热这里有精品视频 | 女子十八毛片 | 一区二区视频在线观看高清视频在线 | 国产精品自在自线 | 婷婷影院在线综合免费视频 | 2022国产91精品久久久久久 | 午夜国产福利在线 | 久久综合丝袜长腿丝袜 | 日本精品久久久久护士 | 国产亚洲高清视频 | 国产精品色综合久久 | 色综合88| 国产一区福利 | 久青草国产手机视频免费观看 | 欧美高清另类videosbestsex | 黄视频网站在线观看 | 超级碰碰青草免费视频92 | 国产真实乱子伦xxxx仙踪 | 国产手机精品视频 | 激情五月婷婷综合 |