亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

The J2EE Tutorial

2019-11-18 16:16:47
字體:
來源:轉載
供稿:網友

  Mapping Table Relationships for Bean-Managed Persistence
In a relational database, tables can be related by common columns. The relationships between the tables affect the design of their corresponding entity beans. The entity beans discussed in this section are backed up by tables with the following types of relationships:

One-to-one
One-to-many
Many-to-many
One-to-One Relationships
In a one-to-one relationship, each row in a table is related to a single row in another table. For example, in a warehouse application, a storagebin table might have a one-to-one relationship with a widget table. This application would model a physical warehouse in which each storage bin contains one type of widget and each widget resides in one storage bin.

Figure 5-1 illustrates the storagebin and widget tables. Because the storagebinid uniquely identifies a row in the storagebin table, it is that table's PRimary key. The widgetid is the primary key of the widget table. The two tables are related because the widgetid is also a column in the storagebin table. By referring to the primary key of the widget table, the widgetid in the storagebin table identifies which widget resides in a particular storage bin in the warehouse. Because the widgetid of the storagebin table refers to the primary key of another table, it is called a foreign key. (The figures in this chapter denote a primary key with PK and a foreign key with FK.)



Figure 5-1 One-to-One Table Relationship

A dependent (child) table includes a foreign key that matches the primary key of the referenced (parent) table. The values of the foreign keys in the storagebin (child) table depend on the primary keys in the widget (parent) table. For example, if the storagebin table has a row with a widgetid of 344, then the widget table should also have a row whose widgetid is 344.

When designing a database application, you may choose to enforce the dependency between the parent and child tables. There are two ways to enforce sUCh a dependency: by defining a referential constraint in the database or by performing checks in the application code. The storagebin table has a referential constraint named fk_widgetid:

CREATE TABLE storagebin
(storagebinid VARCHAR(3)
CONSTRAINT pk_storagebin PRIMARY KEY,
widgetid VARCHAR(3),
quantity INTEGER,
CONSTRAINT fk_widgetid
FOREIGN KEY (widgetid)
REFERENCES widget(widgetid));


The source code for the following example is in the j2eetutorial/examples/src/ejb/storagebin Directory. To compile the code, go to the j2eetutorial/examples directory and type ant storagebin. A sample StorageBinApp.ear file is in the j2eetutorial/examples/ears directory.

The StorageBinBean and WidgetBean classes illustrate the one-to-one relationship of the storagebin and widget tables. The StorageBinBean class contains variables for each column in the storagebin table, including the foreign key, widgetId:

private String storageBinId;
private String widgetId;
private int quantity;


The ejbFindByWidgetId method of the StorageBinBean class returns the storageBinId that matches a given widgetId:

public String ejbFindByWidgetId(String widgetId)
throws FinderException {

String storageBinId;

try {
storageBinId = selectByWidgetId(widgetId);
} catch (Exception ex) {
throw new EJBException("ejbFindByWidgetId: " +
ex.getMessage());
}

if (storageBinId == null) {
throw new ObjectNotFoundException
("Row for widgetId " + widgetId + " not found.");
}
else {
return storageBinId;
}
}


The ejbFindByWidgetId method locates the widgetId by querying the database in the selectByWidgetId method:

private String selectByWidgetId(String widgetId)
throws SQLException {

String storageBinId;

String selectStatement =
"select storagebinid " +
"from storagebin where widgetid = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
prepStmt.setString(1, widgetId);

ResultSet rs = prepStmt.executeQuery();

if (rs.next()) {
storageBinId = rs.getString(1);
}
else {
storageBinId = null;
}

prepStmt.close();
return storageBinId;
}


To find out in which storage bin a widget resides, the StorageBinClient program calls the findByWidgetId method:

String widgetId = "777";
StorageBin storageBin =
storageBinHome.findByWidgetId(widgetId);
String storageBinId = (String)storageBin.getPrimaryKey();
int quantity = storageBin.getQuantity();


Running the StorageBinEJB Example
Create the storagebin database table.
Go to the j2eetutorial/examples directory.
Type ant create-storagebin-table.
Deploy the StorageBinApp.ear file (located in the j2eetutorial/examples/ears directory).
Run the client.
Go to the j2eetutorial/examples/ears directory.
Set the APPCPATH environment variable to StorageBinAppClient.jar.
Type the following command on a single line:
runclient -client StorageBinApp.ear -name
StorageBinClient -textauth


At the login prompts, enter guest for the user name and guest123 for the passWord.
One-to-Many Relationships
If the primary key in a parent table matches multiple foreign keys in a child table, then the relationship is one-to-many. This relationship is common in database applications. For example, an application for a sports league might access a team table and a player table. Each team has multiple players, and each player belongs to a single team. Every row in the child table (player) has a foreign key identifying the player's team. This foreign key matches the team table's primary key.

The sections that follow describe how you might implement one-to-many relationships in entity beans. When designing such entity beans, you must decide whether both tables are represented by entity beans, or just one.

A Helper Class for the Child Table
Not every database table needs to be mapped to an entity bean. If a database table doesn't represent a business entity, or if it stores information that is contained in another entity, then the table should be represented with a helper class. In an online shopping application, for example, each order submitted by a customer can have multiple line items. The application stores the information in the database tables shown by Figure 5-2.



Figure 5-2 One-to-Many Relationship: Order and Line Items

Not only does a line item belong to an order, it also does not exist without the order. Therefore, the lineitems table should be represented with a helper class and not with an entity bean. Using a helper class in this case is not required, but doing so might improve performance because a helper class uses fewer system resources than an entity bean.

The source code for the following example is in the j2eetutorial/examples/src/ejb/order directory. To compile the code, go to the j2eetutorial/examples directory and type ant order. A sample OrderApp.ear file is in the j2eetutorial/examples/ears directory.

The LineItem and OrderBean classes show how to implement a one-to-many relationship with a helper class (LineItem). The instance variables in the LineItem class correspond to the columns in the lineitems table. The itemNo variable matches the primary key for the lineitems table, and the orderId variable represents the table's foreign key. Here is the source code for the LineItem class:

public class LineItem implements java.io.Serializable {

String productId;
int quantity;
double unitPrice;
int itemNo;
String orderId;


public LineItem(String productId, int quantity,
double unitPrice, int itemNo, String orderId) {

this.productId = productId;
this.quantity = quantity;
this.unitPrice = unitPrice;
this.itemNo = itemNo;
this.orderId = orderId;
}

public String getProductId() {
return productId;
}

public int getQuantity() {
return quantity;
}

public double getUnitPrice() {
return unitPrice;
}

public int getItemNo() {
return itemNo;
}

public String getOrderId() {
return orderId;
}
}


The OrderBean class contains an ArrayList variable named lineItems. Each element in the lineItems variable is a LineItem object. The lineItems variable is passed to the OrderBean class in the ejbCreate method. For every LineItem object in the lineItems variable, the ejbCreate method inserts a row into the lineitems table. It also inserts a single row into the orders table. The code for the ejbCreate method follows:

public String ejbCreate(String orderId, String customerId,
String status, double totalPrice, ArrayList lineItems)
throws CreateException {

try {
insertOrder(orderId, customerId, status, totalPrice);
for (int i = 0; i < lineItems.size(); i++) {
LineItem item = (LineItem)lineItems.get(i);
insertItem(item);
}
} catch (Exception ex) {
throw new EJBException("ejbCreate: " +
ex.getMessage());
}

this.orderId = orderId;
this.customerId = customerId;
this.status = status;
this.totalPrice = totalPrice;
this.lineItems = lineItems ;

return orderId;
}


The OrderClient program creates and loads an ArrayList of LineItem objects. The program passes this ArrayList to the entity bean when it invokes the create method:

ArrayList lineItems = new ArrayList();
lineItems.add(new LineItem("p23", 13, 12.00, 1, "123"));
lineItems.add(new LineItem("p67", 47, 89.00, 2, "123"));
lineItems.add(new LineItem("p11", 28, 41.00, 3, "123"));
...
Order duke = home.create("123", "c44", "open",
totalItems(lineItems), lineItems);


Other methods in the OrderBean class also access both database tables. The ejbRemove method, for example, not only deletes a row from the orders table, but also deletes all corresponding rows in the lineitems table. The ejbLoad and ejbStore methods synchronize the state of an OrderEJB instance, including the lineItems ArrayList, with the orders and lineitems tables.

The ejbFindByProductId method enables clients to locate all orders that have a particular product. This method queries the lineitems table for all rows with a specific productId. The method returns a Collection of Order objects. The OrderClient program iterates through the Collection and prints the primary key of each order:

Collection c = home.findByProductId("p67");
Iterator i=c.iterator();
while (i.hasNext()) {
Order order = (Order)i.next();
String id = (String)order.getPrimaryKey();
System.out.println(id);
}


Running the OrderEJB Example
Create the orders database table:.
Go to the j2eetutorial/examples directory.
Type ant create-order-table.
Deploy the OrderApp.ear file (located in the j2eetutorial/examples/ears directory).
Run the client.
Go to the j2eetutorial/examples/ears directory.
Set the APPCPATH environment variable to OrderAppClient.jar.
Type the following command on a single line:
runclient -client OrderApp.ear -name OrderClient
-textauth


At the login prompts, enter guest for the user name and guest123 for the password.
An Entity Bean for the Child Table
You should consider building an entity bean for a child table under the following conditions:

The information in the child table is not dependent on the parent table.
The business entity of the child table could exist without that of the parent table.
The child table might be accessed by another application that does not access the parent table.
These conditions exist in the following scenario. Suppose that each sales representative in a company has multiple customers and that each customer has only one sales representative. The company tracks its sales force with a database application. In the database, each row in the salesrep table (parent) matches multiple rows in the customer table (child). Figure 5-3 illustrates this relationship.



Figure 5-3 One-to-Many Relationship: Sales Representative and Customers

The SalesRepBean and CustomerBean entity bean classes implement the one-to-many relationship of the sales and customer tables.

The source code for this example is in the j2eetutorial/examples/src/ejb/salesrep directory. To compile the code, go to the j2eetutorial/examples directory and type ant salesrep. A sample SalesRepApp.ear file is in the j2eetutorial/examples/ears directory.

The SalesRepBean class contains a variable named customerIds, which is an ArrayList of String elements. These String elements identify which customers belong to the sales representative. Because the customerIds variable reflects this relationship, the SalesRepBean class must keep the variable up to date.

The SalesRepBean class instantiates the customerIds variable in the setEntityContext method, not in ejbCreate. The container invokes setEntityContext just once--when it creates the bean instance--ensuring that customerIds is instantiated just once. Because the same bean instance can assume different identities during its life cycle, instantiating customerIds in ejbCreate might cause multiple and unnecessary instantiations. Therefore, the SalesRepBean class instantiates the customerIds variable in setEntityContext:

public void setEntityContext(EntityContext context) {

this.context = context;
customerIds = new ArrayList();

try {
makeConnection();
Context initial = new InitialContext();
Object objref =
initial.lookup("java:comp/env/ejb/Customer");

customerHome =
(CustomerHome)PortableRemoteObject.narrow(objref,
CustomerHome.class);
} catch (Exception ex) {
throw new EJBException("setEntityContext: " +
ex.getMessage());
}
}


Invoked by the ejbLoad method, loadCustomerIds is a private method that refreshes the customerIds variable. There are two approaches when coding a method such as loadCustomerIds: fetch the identifiers from the customer database table or get them from the CustomerEJB entity bean. Fetching the identifiers from the database might be faster, but eXPoses the code in the SalesRepBean class to the CustomerEJB bean's underlying database table. In the future, if you were to change the CustomerEJB bean's table (or move the bean to a different J2EE server), you might need to change the SalesRepBean code. But if the SalesRepBean class gets the identifiers from the CustomerEJB entity bean, no coding changes would be required. The two approaches present a trade-off: performance versus flexibility. The SalesRepEJB example opts for flexibility, loading the customerIds variable by calling the findSalesRep and getPrimaryKey methods of CustomerEJB. Here is the code for the loadCustomerIds method:

private void loadCustomerIds() {

customerIds.clear();

try {
Collection c = customerHome.findBySalesRep(salesRepId);
Iterator i=c.iterator();

while (i.hasNext()) {
Customer customer = (Customer)i.next();
String id = (String)customer.getPrimaryKey();
customerIds.add(id);
}

} catch (Exception ex) {
throw new EJBException("Exception in loadCustomerIds: " +
ex.getMessage());
}
}


If a customer's sales representative changes, the client program updates the database by calling the setSalesRepId method of the CustomerBean class. The next time a business method of the SalesRepBean class is called, the ejbLoad method invokes loadCustomerIds, which refreshes the customerIds variable. (To ensure that ejbLoad is invoked before each business method, set the transaction attributes of the business methods to Required.) For example, the SalesRepClient program changes the salesRepId for a customer named Mary Jackson as follows:

Customer mary = customerHome.findByPrimaryKey("987");
mary.setSalesRepId("543");


The salesRepId value 543 identifies a sales representative named Janice Martin. To list all of Janice's customers, the SalesRepClient program invokes the getCustomerIds method, iterates through the ArrayList of identifiers, and locates each CustomerEJB entity bean by calling its findByPrimaryKey method:

SalesRep janice = salesHome.findByPrimaryKey("543");
ArrayList a = janice.getCustomerIds();
i = a.iterator();

while (i.hasNext()) {
String customerId = (String)i.next();
Customer customer =
customerHome.findByPrimaryKey(customerId);
String name = customer.getName();
System.out.println(customerId + ": " + name);
}


Running the SalesRepEJB Example
Create the database tables.
Go to the j2eetutorial/examples/src directory.
Type ant create-salesrep-table.
Deploy the SalesRepApp.ear file (located in the j2eetutorial/examples/ears directory).
Run the client.
Go to the j2eetutorial/examples/ears directory.
Set the APPCPATH environment variable to SalesRepAppClient.jar.
Type the following command on a single line:
runclient -client SalesRepApp.ear -name SalesRepClient
-textauth


At the login prompts, enter guest for the user name and guest123 for the password.
Many-to-Many Relationships
In a many-to-many relationship, each entity may be related to multiple occurrences of the other entity. For example, a college course has many students and each student may take several courses. In a database, this relationship is represented by a cross reference table containing the foreign keys. In Figure 5-4, the cross reference table is the enrollment table. These tables are accessed by the StudentBean, CourseBean, and EnrollerBean classes.



Figure 5-4 Many-to-Many Relationship: Students and Courses

The source code for this example is in the j2eetutorial/examples/src/ejb/enroller directory. To compile the code, go to the j2eetutorial/examples directory and type ant enroller. A sample EnrollerApp.ear file is in the j2eetutorial/examples/ears directory.

The StudentBean and CourseBean classes are complementary. Each class contains an ArrayList of foreign keys. The StudentBean class contains an ArrayList named courseIds, which identifies the courses the student is enrolled in. Likewise, the CourseBean class contains an ArrayList named studentIds.

The ejbLoad method of the StudentBean class adds elements to the courseIds ArrayList by calling loadCourseIds, a private method. The loadCourseIds method gets the course identifiers from the EnrollerEJB session bean. The source code for the loadCourseIds method follows:

private void loadCourseIds() {

courseIds.clear();

try {
Enroller enroller = enrollerHome.create();
ArrayList a = enroller.getCourseIds(studentId);
courseIds.addAll(a);

} catch (Exception ex) {
throw new EJBException("Exception in loadCourseIds: " +
ex.getMessage());
}
}


Invoked by the loadCourseIds method, the getCourseIds method of the EnrollerBean class queries the enrollment table:

select courseid from enrollment
where studentid = ?


Only the EnrollerBean class accesses the enrollment table. Therefore, the EnrollerBean class manages the student-course relationship represented in the enrollment table. If a student enrolls in a course, for example, the client calls the enroll business method, which inserts a row:

insert into enrollment
values (studentid, courseid)


If a student drops a course, the unEnroll method deletes a row:

delete from enrollment
where studentid = ? and courseid = ?


And if a student leaves the school, the deleteStudent method deletes all rows in the table for that student:

delete from enrollment
where student = ?


The EnrollerBean class does not delete the matching row from the student table. That action is performed by the ejbRemove method of the StudentBean class. To ensure that both deletes are executed as a single Operation, they should belong to the same transaction. See Chapter 14 for more information.

Running the EnrollerEJB Example
Create the database tables.
Go to the j2eetutorial/examples directory.
Type ant create-enroller-table.
Deploy the EnrollerApp.ear file (located in the j2eetutorial/examples/ears directory).
Run the client.
Go to the j2eetutorial/examples/ears directory.
Set the APPCPATH environment variable to EnrollerAppClient.jar.
Type the following command on a single line:
runclient -client EnrollerApp.ear -name EnrollerClient
-textauth


At the login prompts, enter guest for the user name and guest123 for the password.

(出處:http://www.49028c.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
在线观看久久av| 亚洲欧美国内爽妇网| 精品无码久久久久久国产| 精品日韩视频在线观看| 91产国在线观看动作片喷水| 国模极品一区二区三区| 久久精品国产96久久久香蕉| 黑人极品videos精品欧美裸| 神马国产精品影院av| 欧美电影免费在线观看| 亚洲一区久久久| 欧美在线免费视频| 国产a∨精品一区二区三区不卡| 亚洲精品视频中文字幕| 日韩中文字幕在线播放| 日韩在线观看成人| 精品色蜜蜜精品视频在线观看| 欧美一乱一性一交一视频| 欧美激情视频免费观看| 欧美性猛交xxxx| 九九精品在线视频| 国产精品久久久| 亚洲第一二三四五区| 日韩在线观看免费全| 欧美激情xxxxx| 欧美日韩午夜视频在线观看| 色噜噜狠狠狠综合曰曰曰| 国产91成人video| 欧美福利在线观看| 日韩亚洲一区二区| 91精品成人久久| 在线视频日本亚洲性| 欧美在线观看一区二区三区| 国产欧美亚洲精品| 欧美极品美女电影一区| 91美女片黄在线观| 欧美国产日韩免费| 色综合天天综合网国产成人网| 国产精品视频播放| 国产suv精品一区二区三区88区| 欧美日韩激情视频| 精品国产欧美一区二区五十路| 久久久久国产精品一区| 国产一区二区三区在线看| 欧美专区在线播放| 粗暴蹂躏中文一区二区三区| 久久视频精品在线| 欧美性猛交xxxx乱大交极品| 亚洲欧美另类国产| 粉嫩av一区二区三区免费野| 国产欧美婷婷中文| 国产精品视频播放| 亚洲男人天堂手机在线| 米奇精品一区二区三区在线观看| 日韩av三级在线观看| 一夜七次郎国产精品亚洲| 亚洲精品一区二区在线| yellow中文字幕久久| 亚洲国产另类 国产精品国产免费| 亚洲精品电影在线观看| 亚洲偷熟乱区亚洲香蕉av| 欧美国产日本高清在线| 国产精品户外野外| 亚洲国产精品专区久久| 久久艹在线视频| 狠狠躁夜夜躁人人爽天天天天97| 欧美日韩一区免费| 亚洲一区二区三区久久| 日韩一中文字幕| 久久久久久高潮国产精品视| 97久久精品国产| 91av视频在线免费观看| 成人久久久久久久| 国产成人亚洲综合91| 国产精品综合久久久| 成人黄色激情网| 日日摸夜夜添一区| 久久久久久成人| 欧美精品videos| 美女黄色丝袜一区| 亚洲国产精品中文| 亚洲日本aⅴ片在线观看香蕉| 久久久久亚洲精品成人网小说| 亚洲香蕉成人av网站在线观看| 曰本色欧美视频在线| 国产成人黄色av| 91久久精品国产91性色| 狠狠色狠色综合曰曰| 亚洲午夜av电影| 亚洲欧美激情另类校园| 狠狠综合久久av一区二区小说| 亚洲精品一区二区三区婷婷月| 亚洲精品免费在线视频| 日本乱人伦a精品| 大伊人狠狠躁夜夜躁av一区| 国产精品r级在线| 福利视频一区二区| 久久久亚洲国产| 高清亚洲成在人网站天堂| 亚洲成年网站在线观看| 亚洲天堂免费视频| 欧美激情欧美激情| 亚洲欧美国内爽妇网| 精品一区二区三区四区在线| 亚洲a在线观看| 色偷偷888欧美精品久久久| 亚洲深夜福利视频| 亚洲欧美日韩久久久久久| 欧美日韩精品在线播放| 欧美精品www| 日韩久久免费电影| 亚洲乱码国产乱码精品精| 日韩在线观看免费av| 欧美一二三视频| 国产视频精品在线| 久久国内精品一国内精品| 欧美电影免费在线观看| 91夜夜未满十八勿入爽爽影院| 日韩欧美高清在线视频| 亚洲女成人图区| 国产欧美精品一区二区三区介绍| 日韩av一区在线| 国产成人av网址| 色婷婷综合成人av| 亚洲成人网久久久| 欧美综合激情网| 亚洲新中文字幕| 青青久久av北条麻妃海外网| 欧美视频不卡中文| 日韩欧美中文第一页| 国产精品成人一区| 福利一区福利二区微拍刺激| 欧美区二区三区| 色妞色视频一区二区三区四区| 日韩av免费一区| 亚洲97在线观看| 国产精品视频播放| 成人自拍性视频| 最近2019免费中文字幕视频三| 国产精品自拍网| 久久人人爽亚洲精品天堂| 91美女片黄在线观| 欧美乱人伦中文字幕在线| 欧美日韩午夜视频在线观看| 日韩欧美在线国产| 亚洲日韩中文字幕在线播放| 成人免费在线视频网站| 欧美成人午夜影院| 久久99精品久久久久久琪琪| 日韩在线视频观看| 精品视频www| 色哟哟亚洲精品一区二区| 国产精品久久久久影院日本| 91精品在线影院| 欧美多人爱爱视频网站| 久久久久久午夜| 精品视频—区二区三区免费| 欧美成人全部免费| 亚洲国产成人精品久久| 国产欧美日韩中文字幕在线| 一区二区日韩精品| 深夜福利一区二区| 久久久噜噜噜久噜久久| 欧美超级乱淫片喷水|