?? 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 + -