主頁 > 知識(shí)庫 > EJB3.0開發(fā)之多對(duì)多和一對(duì)一

EJB3.0開發(fā)之多對(duì)多和一對(duì)一

熱門標(biāo)簽:地圖標(biāo)注人員兼職 開封便宜外呼系統(tǒng)報(bào)價(jià) 電話機(jī)器人的特色和創(chuàng)新 騰訊地圖標(biāo)注商戶改名注冊(cè)入駐 黃石智能營(yíng)銷電銷機(jī)器人效果 商丘百應(yīng)電話機(jī)器人有沒有效果 怎樣把地圖標(biāo)注出來 淮南騰訊地圖標(biāo)注 漯河辦理400電話
在前面的例子中,我們演示了一對(duì)多和多對(duì)一的例子,在本章將演示多對(duì)多和一對(duì)一的關(guān)系。

  學(xué)生和老師就是多對(duì)多的關(guān)系。一個(gè)學(xué)生有多個(gè)老師,一個(gè)老師教多個(gè)學(xué)生。

  學(xué)生和檔案就是一對(duì)一的關(guān)系(不知道國(guó)外的學(xué)生有沒有檔案?)。

  為了實(shí)現(xiàn)多對(duì)多的關(guān)系,數(shù)據(jù)庫中需要關(guān)聯(lián)表,用以在兩個(gè)實(shí)體間建立關(guān)聯(lián)。JBoss可以自動(dòng)生成關(guān)聯(lián)表,你也可以@AssociationTable來指定關(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)表的名稱、主表的列和從表的列。

  為了實(shí)現(xiàn)一對(duì)一的關(guān)系,需要用@OneToOne來注釋。

  如:

  @OneToOne(cascade = {CascadeType.ALL})
  @JoinColumn(name = "DOSSIER_ID")

  public Dossier getDossier()
  {
  return dossier;
  }

  這定義了一個(gè)單向的一對(duì)一的關(guān)系。如果在Dossier也定義了相關(guān)的關(guān)聯(lián),那么它就是雙向的。雙向的意思就是通過一個(gè)Student實(shí)體就可以查找到一個(gè)Dossier,通過一個(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所依賴的類。

  Teacher.java:實(shí)體Bean所依賴的類。

  EntityTest.java:會(huì)話Bean的業(yè)務(wù)接口

  EntityTest Bean.java:會(huì)話Bean的實(shí)現(xiàn)類

  Client.java:測(cè)試EJB的客戶端類。

  jndi.properties:jndi屬性文件,提供訪問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 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("胡時(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è)客戶端用來測(cè)試。

  請(qǐng)運(yùn)行{$JBOSS_HOME}/bin目錄下的run.bat: run –c all,啟動(dòng)JBOSS。

  http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBeanname=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB,然后調(diào)用startDatabaseManager()方法,打開HSQL管理工具管理數(shù)據(jù)庫。

  在Eclipse的Ant視圖中執(zhí)行ejbjar target。或者在命令行下,進(jìn)入到此工程目錄下,執(zhí)行ant ejbjar,將編譯打包發(fā)布此EJB。

  在Eclipse的Ant視圖中執(zhí)行run target?;蛘咴诿钚邢?,進(jìn)入到此工程目錄下,執(zhí)行ant run,測(cè)試這個(gè)EJB。
您可能感興趣的文章:
  • JavaBean(EJB) 3.0 全新體驗(yàn)
  • EJB 3.0 開發(fā)指南之定時(shí)服務(wù)
  • EJB 3.0開發(fā)指南之多表映射
  • EJB3.0之實(shí)體Bean的繼承
  • EJB3.0部署消息驅(qū)動(dòng)Bean拋javax.naming.NameNotFoundException異常

標(biāo)簽:岳陽 拉薩 馬鞍山 大興安嶺 武威 紅河 鄭州 亳州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《EJB3.0開發(fā)之多對(duì)多和一對(duì)一》,本文關(guān)鍵詞  EJB3.0,開,發(fā)之,多對(duì),多,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《EJB3.0開發(fā)之多對(duì)多和一對(duì)一》相關(guān)的同類信息!
  • 本頁收集關(guān)于EJB3.0開發(fā)之多對(duì)多和一對(duì)一的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章