EJB3.0开发之多对多和一对一
程序员文章站
2024-02-22 18:10:52
在前面的例子中,我们演示了一对多和多对一的例子,在本章将演示多对多和一对一的关系。 学生和老师就是多对多的关系。一个学生有多个老师,一个老师教多个学生。 学生和档案...
在前面的例子中,我们演示了一对多和多对一的例子,在本章将演示多对多和一对一的关系。
学生和老师就是多对多的关系。一个学生有多个老师,一个老师教多个学生。
学生和档案就是一对一的关系(不知道国外的学生有没有档案?)。
为了实现多对多的关系,数据库中需要关联表,用以在两个实体间建立关联。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。
上一篇: Android百度地图poi范围搜索
推荐阅读
-
EJB3.0开发之多对多和一对一
-
SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送
-
mybatis 一对一、一对多和多对多查询实例代码
-
mybatis 一对一、一对多和多对多查询实例代码
-
EJB3.0开发之多对多和一对一
-
Mybatis04—注解开发实现CRUD以及实现一对一、一对多及多对多复杂关系映射
-
Mybatis常用的注解开发CRUD&&复杂关系映射(一对一,一对多)&&mybatis 基于注解的二级缓存
-
mybatis延迟加载一对一和一对多
-
php在写数据中时,啥叫一对一和一对多
-
SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送