DDR爱好者之家 Design By 杰米
在前面的例子中,我们演示了一对多和多对一的例子,在本章将演示多对多和一对一的关系。
学生和老师就是多对多的关系。一个学生有多个老师,一个老师教多个学生。
学生和档案就是一对一的关系(不知道国外的学生有没有档案?)。
为了实现多对多的关系,数据库中需要关联表,用以在两个实体间建立关联。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的方法,并提供了查找老师的方法。
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 all,启动JBOSS。
http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB,然后调用startDatabaseManager()方法,打开HSQL管理工具管理数据库。
在Eclipse的Ant视图中执行ejbjar target。或者在命令行下,进入到此工程目录下,执行ant ejbjar,将编译打包发布此EJB。
在Eclipse的Ant视图中执行run target。或者在命令行下,进入到此工程目录下,执行ant run,测试这个EJB。
学生和老师就是多对多的关系。一个学生有多个老师,一个老师教多个学生。
学生和档案就是一对一的关系(不知道国外的学生有没有档案?)。
为了实现多对多的关系,数据库中需要关联表,用以在两个实体间建立关联。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 all,启动JBOSS。
http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB,然后调用startDatabaseManager()方法,打开HSQL管理工具管理数据库。
在Eclipse的Ant视图中执行ejbjar target。或者在命令行下,进入到此工程目录下,执行ant ejbjar,将编译打包发布此EJB。
在Eclipse的Ant视图中执行run target。或者在命令行下,进入到此工程目录下,执行ant run,测试这个EJB。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年12月26日
2024年12月26日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]