Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Bối cảnh ban đầu = new InitialContext (); đối tượng objref = initial.lookup ("java: comp / env / ejb / khách hàng"); customerHome = (CustomerHome) PortableRemoteObject.narrow (objref, CustomerHome.class);} catch (Exception ex) {ném mới EJBException ("setEntityContext:" + ex.getMessage ());}} | 106 Entity Beans 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 loadEnrollerIds 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 Customer entity bean. Fetching the identifiers from the database might be faster but exposes the SalesRepEJB code to the Customer bean s underlying database table. In the future if you were to change the Customer bean s table or move the bean to a different J2EE server then you might need to change the SalesRepEJB code. But if the SalesRepEJB gets the identifiers from the Customer 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 the Customer bean. 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 Mapping Table Relationships For Bean-Managed Persis- If a customer s sales representative changes the client program updates the database by calling the setSalesRepId method of the CustomerEJB class. The next time a business method of the SalesRepEJB is called the ejbLoad method invokes loadCustomerlds which refreshes the customerlds variable. To ensure that ejbLoad is invoked before each business method set the transaction attributes of the business .