📄 cmprelationships.txt
字号:
一. 在JBuilder中创建cmp entity bean的一对一关系:
1.创建一个sql文件:
File->New File填入或选择:
Name: order Type: sql
->OK
order.sql的内容如下:
create table order(
orderid varchar2(16) primary key,
ordername varchar2(64) not null
);
create table shipment(
shipmentid varchar2(16) primary key,
city varchar2(32) not null,
zip varchar2(16),
orderid varchar2(16) references order(orderid)
);
2. 将以上sql文件中的表创建到数据库中:
Tools->DataBase Pilot
选择一个数据库URL,按右键选择open弹出一对话框,输入
User Name: <your db user name>
Password: <your db user password>
->OK
选择Enter SQL->Load SQL...选择文件order.sql
按Execute执行sql创建表后退出。
3. 创建一个新的EJB Module
File->New->Enterprise->EJB Module,输入以下信息:
Name: cmprelationships Format: XML
Version: EJB 2.0 Compliant
->OK
4. 引入Database schema
在JBUILDER左下框选择DataSources,按右键选择add datasource,
data source名字是weblogic配置的data source的JNDI名,
如jdbc/roster,
选择jdbc/roster->Properties,出现对话框:
Driver: com.pointbase.jdbc.jdbcUniversalDriver
URL: jdbc:pointbase:server://localhost:9092/sample
User: pbpublic
Password: pbpublic
JNDI name: <weblogic配置的DataSource JNDI名字jdbc/roster>
点击OK
5.创建CMP Entity Bean: OrderEJB, ShipmentEJB
在JBUILDER左下框选择表order和shipment,按右键选择
Create CMP 2.0 Entity Bean
6. 修改新生成的Entity Beans全局属性
在EJB Designer中选择新生成的Entity Bean Order,出现一对话框,
修改如下信息:
Bean Name: OrderEJB
Abstract Schema Name: Order
Interfaces: Local
点击Classes and Packages...出现新对话框, 修改如下信息:
Default Package: order.ejb
Bean Name: OrderEJB
Bean Class: order.ejb.OrderBean
Local Home interface class: order.ejb.OrderLocalHome
Local interface class: order.ejb.OrderLocal
->OK
在EJB Designer中选择新生成的Entity Bean Shipment,出现一对话框,
修改如下信息:
Bean Name: ShipmentEJB
Abstract Schema Name: Shipment
Interfaces: Local
点击Classes and Packages...出现新对话框, 修改如下信息:
Default Package: order.ejb
Bean Name: ShipmentEJB
Bean Class: order.ejb.ShipmentBean
Local Home interface class: order.ejb.ShipmentLocalHome
Local interface class: order.ejb.ShipmentLocal
->OK
7. 修改OrderEJB和ShipmentEJB的cmp-field属性:
在EJB Designer中选择OrderEJB, 点击orderName,出现对话框修改
In ejbCreate: true
在EJB Designer中选择ShipmentEJB, 点击city,出现对话框修改
In ejbCreate: true
点击zip, 出现对话框修改
In ejbCreate: true
点击orderid按右键选择Delete field,选择Yes确认。
8.添加关系:
注意:在JBuilder中关系只能从主表对应的Entity Bean发起。
选择OrderEJB,按右键选择Add->Relationship,将箭头指向ShipmentEJB,
点击OrderEJB中的关系域,出现一对话框,修改属性:
Relationship Properties:
Relationship Name: <不要改动>
Multiplicity: one to one
Navigability: unidirectional
Cascade delete: true
db-cascade-delete: true
CMR field properties:
Field name: shipment
Return type: bean instance
Getters: local
Setters: local
点击Edit RDBMS Relation...出现一对话框,确认一条连线连接
order(orderid)与shipment(orderid)
9. 在OrderEJB中添加一Finder方法findByCity(String city)返回发货到
某一城市的所有定单:
在EJB Designer中OrderEJB上按右键选择
Add->Finder出现对话框,修改以下信息:
Finder Name: findByCity
Return Type: java.util.Collection
Input Parameter: String city
Query: select distinct object(o) from Order o where
o.shipment.city=?1
->OK
二. 在JBuilder中创建cmp entity bean的一对多关系:
1.创建一个sql文件:
File->New File填入或选择:
Name: company Type: sql
->OK
company.sql的内容如下:
create table company(
company_id varchar2(16) primary key,
name varchar2(128) not null
);
create table employee(
employee_id varchar2(16) primary key,
name varchar2(32) not null,
sex varchar2(8) not null,
company_id varchar2(16) references company(company_id)
);
2. 创建表,步骤如同一(2),Load SQL...时选择company.sql
3. 引入新的表company和employee
在JBuilder的左上框中双击EJB Module cmprelationships, 在右下
框中选择<<一>>中创建的data source, 按右键选择Refresh from database
4. 创建CMP Entity Bean: CompanyEJB, EmployeeEJB
在JBUILDER左下框选择表company和employee,按右键选择
Create CMP 2.0 Entity Bean
5. 修改新生成的Entity Beans全局属性
在EJB Designer中选择新生成的Entity Bean Company,出现一对话框,
修改如下信息:
Bean Name: CompanyEJB
Abstract Schema Name: Company
Interfaces: Local
点击Classes and Packages...出现新对话框, 修改如下信息:
Default Package: company.ejb
Bean Name: CompanyEJB
Bean Class: company.ejb.CompanyBean
Local Home interface class: company.ejb.CompanyLocalHome
Local interface class: company.ejb.CompanyLocal
->OK
在EJB Designer中选择新生成的Entity Bean Employee,出现一对话框,
修改如下信息:
Bean Name: EmployeeEJB
Abstract Schema Name: Employee
Interfaces: Local
点击Classes and Packages...出现新对话框, 修改如下信息:
Default Package: company.ejb
Bean Name: EmployeeEJB
Bean Class: company.ejb.EmployeeBean
Local Home interface class: company.ejb.EmployeeLocalHome
Local interface class: company.ejb.EmployeeLocal
->OK
6. 修改CompanyEJB和EmployeeEJB的cmp-field属性:
在EJB Designer中选择CompanyEJB, 点击name,出现对话框修改
In ejbCreate: true
在EJB Designer中选择EmployeeEJB, 点击name,出现对话框修改
In ejbCreate: true
点击sex, 出现对话框修改
In ejbCreate: true
点击companyId按右键选择Delete field,选择Yes确认。
7.添加关系:
注意:在JBuilder中关系只能从主表对应的Entity Bean发起。
选择CompanyEJB,按右键选择Add->Relationship,将箭头指向EmployeeEJB,
点击CompanyEJB中的关系域,出现一对话框,修改属性:
Relationship Properties:
Relationship Name: <不要改动>
Multiplicity: one to many
Navigability: bidirectional
Cascade delete: false
db-cascade-delete: false
CMR field properties:
Field name: employees
Return type: java.util.Collection
Getters: local
Setters: local
点击Edit RDBMS Relation...出现一对话框,先点击按钮Clear,
再用鼠标选择company(company_id),按住鼠标左键连接到
employee(company_id)
->OK
选择EmployeeEJB中的关系域出现一对话框,修改:
CMR field properties:
Field name: company
8.在CompanyEJB中添加Finder方法:
在EJB Designer中CompanyEJB上按右键选择
Add->Finder出现对话框,修改以下信息:
Finder Name: findByEmployeeName
Return Type: java.util.Collection
Input parameter: String
Query: select distinct object(c) from Company c,
in (c.employees) as e where e.name=?1
->OK
9.在CompanyEJB中添加商业方法:
在EJB Designer中CompanyEJB上按右键选择
Add->Method出现对话框,修改以下信息:
Method Name: hire
Return Type: void
Input Parameters: EmployeeLocal emp
Interfaces: Local
重复上面动作添加fire方法:
Method Name: fire
Return Type: void
Input Parameters: EmployeeLocal emp
Interfaces: Local
10. 修改CompanyEJB的Bean Class: CompanyBean
在EJB Designer中CompanyEJB上按右键选择View Bean Source
修改hire和fire方法:
public void hire(EmployeeLocal emp){
getEmployees().add(emp);
}
public void fire(EmployeeLocal emp){
getEmployees().remove(emp);
}
三. 在JBuilder中创建cmp entity bean的多对多关系:
1.创建一个sql文件:
File->New File填入或选择:
Name: enrollment Type: sql
->OK
enrollment.sql的内容如下:
create table tstudent(
sid varchar2(16) primary key,
name varchar2(64) not null
);
create table tcourse(
cid varchar2(16) primary key,
name varchar2(64) not null,
desc varchar2(256)
);
create table enrollment(
sid varchar2(16) references tstudent(sid),
cid varchar2(16) references tcourse(cid),
constraint pk primary key (sid, cid)
);
2. 创建表,步骤如同一(2),Load SQL...时选择enrollment.sql
3. 引入新的表tstudent, tcourse和enrollment
在JBuilder的左上框中双击EJB Module cmprelationships, 在右下
框中选择<<一>>中创建的data source, 按右键选择Refresh from database
4. 创建CMP Entity Bean: StudentEJB, CourseEJB
在JBUILDER左下框选择表tstudent和tcourse,按右键选择
Create CMP 2.0 Entity Bean
5. 修改新生成的Entity Beans全局属性
在EJB Designer中选择新生成的Entity Bean Tstudent,出现一对话框,
修改如下信息:
Bean Name: StudentEJB
Abstract Schema Name: Student
Interfaces: Local
点击Classes and Packages...出现新对话框, 修改如下信息:
Default Package: enrollment.ejb
Bean Name: StudentEJB
Bean Class: enrollment.ejb.StudentBean
Local Home interface class: enrollment.ejb.StudentLocalHome
Local interface class: enrollment.ejb.StudentLocal
->OK
在EJB Designer中选择新生成的Entity Bean Tcourse,出现一对话框,
修改如下信息:
Bean Name: CourseEJB
Abstract Schema Name: Course
Interfaces: Local
点击Classes and Packages...出现新对话框, 修改如下信息:
Default Package: enrollment.ejb
Bean Name: CourseEJB
Bean Class: enrollment.ejb.CourseBean
Local Home interface class: enrollment.ejb.CourseLocalHome
Local interface class: enrollment.ejb.CourseLocal
->OK
6. 修改StudentEJB和CourseEJB的cmp-field属性:
在EJB Designer中选择StudentEJB, 点击name,出现对话框修改
In ejbCreate: true
在EJB Designer中选择CourseEJB, 点击name,出现对话框修改
In ejbCreate: true
7.添加关系:
选择StudentEJB,按右键选择Add->Relationship,将箭头指向CourseEJB,
点击StudentEJB中的关系域,出现一对话框,修改属性:
Relationship Properties:
Relationship Name: <不要改动>
Multiplicity: many to many
Navigability: bidirectional
CMR field properties:
Field name: courses
Return type: java.util.Collection
Getters: local
Setters: local
点击Edit RDBMS Relation...出现一对话框,在对话框中选择中间表为
enrollment,用鼠标选择tstudent(sid),按住鼠标左键连接到
enrollment(sid);用鼠标选择tcourse(cid),按住鼠标左键连接到
enrollment(cid)
->OK
选择CourseEJB中的关系域出现一对话框,修改:
CMR field properties:
Field name: students
8.在StudentEJB中添加Finder方法:
在EJB Designer中StudentEJB上按右键选择
Add->Finder出现对话框,修改以下信息:
Finder Name: findByCourse
Return Type: java.util.Collection
Input parameters: CourseLocal c
Query: select distinct object(s) from Student s,
in (s.courses) as c where c=?1
->OK
重复以上步骤加入finder方法findWithoutCourses
Finder Name: findWithoutCourses
Return Type: java.util.Collection
Input parameters:
Query: select distinct object(s) from Student s
where s.courses is empty
->OK
在CourseEJB中添加finder方法findNoStudents:
在EJB Designer中CourseEJB上按右键选择
Add->Finder出现对话框,修改以下信息:
Finder Name: findNoStudents
Return Type: java.util.Collection
Input parameters:
Query: select distinct object(c) from Course c
where c.students is empty
->OK
9.在StudentEJB中添加商业方法:
在EJB Designer中StudentEJB上按右键选择
Add->Method出现对话框,修改以下信息:
Method Name: register
Return Type: void
Input Parameters: CourseLocal course
Interfaces: Local
重复上面动作添加withdraw方法:
Method Name: withdraw
Return Type: void
Input Parameters: CourseLocal course
Interfaces: Local
10.在StudentEJB中添加home方法:
在EJB Designer中StudentEJB上按右键选择
Add->Method出现对话框,修改以下信息:
Method Name: getStudentAmtWithCourse
Return Type: int
Input Parameters: String courseName
Interfaces: Local home
11.在StudentEJB中添加select方法辅助home方法
getStudentAmtWithCourse的实现:
在EJB Designer中StudentEJB上按右键选择
Add->Select出现对话框,修改以下信息:
Select Name: selectStudentAmtWithCourse
Return Type: int
Input Parameters: String courseName
EJB QL: select count(s) from Student s,
in (s.courses) as c where c.name=?1
12. 修改StudentEJB的Bean Class: StudentBean
在EJB Designer中StudentEJB上按右键选择View Bean Source
修改register, withdraw和ejbHomeGetStudentAmtWithCourse方法:
public void register(CourseLocal course){
getCourses().add(course);
}
public void withdraw(CourseLocal course){
getCourses().remove(course);
}
public int ejbHomeGetStudentAmtWithCourse(String courseName){
try{
return ejbSelectStudentAmtWithCourse(courseName);
}catch(FinderException fe){
fe.printStackTrace();
throw new javax.ejb.EJBException(fe.getMessage());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -