One:
The first design pattern is very simple. The Entity Bean of a company and the employee and the code segment of the Entity Bean given below are similar. They are generated by the EntityBean template of JBuilder4. All fields are declared as public CMP fields.
Code snippet for Company Entity Beanpublic class CompanyBean implements EntityBean {EntityContext entityContext; public Integer comId; // the primary keypublic String comName; // the company namepublic String comDescription // basic descriptionpublic Timestamp mutationDate // explained laterpublic Integer ejbCreate (
// various get () and set () for every column / field // Which is Well This design mode is very simple, but there are many shortcomings, for example, access to each field will lead to A remote call for the get () and set () methods. Remote Process Call (RPCS) is a very resource-consuming, and access to a series of remote calls for a combination in the actual requirements. It can be said that this mode is very available in actual use. The design pattern shown above can be used as the foundation of other design patterns, such as RAD, prototype design, testing, etc. At this time, the Employee Estity Bean representing the employee did not show what relations between employees and companies. 2:
In order to avoid the shortcomings of design mode 1, we will introduce the concept of Value Object in encapsulating the entity bean value. Value Object, using some languages, is a structural type because they are very similar to the structure type of CORBA.
value Object code snippet for Companypublic class CompanyStruct implementsjava.io.Serializable {public Integer comId; // Primary Keypublic String comName; public String comDescription; public java.sql.Timestamp mutationDate;} value Object code snippet for Employeepublic class EmployeeStruct implementsjava.io. Serializable {public Integer empId; // Primary Keypublic Integer comId; // Foreign Keypublic String empFirstName; public String empLastName; public java.sql.Timestamp mutationDate;} now, company employees and entity bean can be a structure type as above ejbCreate A parameter of (). Since this structure encapsulates the value of all the fields of Entity, the Entity Bean only needs to operate all the fields for a getData () and setData () methods.
Code snippet for an Entity Bean's create () public Integer ejbCreate (CompanyStruct struct) throwsCreateException {this.comId = struct.comId; this.comName = struct.comName; this.comDescription = struct.comDescription; this.mutationDate = struct.mutationDate; return null;} Code snippet for an Entity Bean's getData () public CompanyStruct getData () {CompanyStruct result = new CompanyStruct (); result.comId = this.comId; result.comName = this.comName; result.comDescription = this.comDescription ; result.mutationDate = this.mutationDate; return result;} Code snippet for an Entity Bean's setData () public void setData (CompanyStruct struct) {this.comName = struct.comName; this.comDescription = struct.comDescription; this.mutationDate = Struct.mutationDate ;;}
The specific fields are used in the design mode 1 to operate the specific fields. In the design mode 2, we avoid this situation and only need a remote call. Now, only one transaction operates all the data through a remote call. In this way, we avoid most of the shortcomings of design mode 1, in addition to establishing the relationship between Beans. Although the setData () method assigns all fields, Borland Appserver provides a smart update feature, only the modified field is rewritten, if there is no field being modified, then the EJBStore () method will Will be skipped. The Borland Programmer Development Guide (EJB) has a more detailed description. Similarly, this duplicate code is present between the Entity Bean and Struct, such as the same field declaration. This means that the modification of any database table structure will cause Entity Beabn and Struct to change, which makes synchronous Entity and Struct become difficult. That is to call the setData () method in the ebcreate () method, which eliminates some redundant code. Code snippet for an Entity Bean's create () public Integer ejbCreate (CompanyStruct struct) throwsCreateException {this.comId = struct.comId; // set the primary keysetData (struct); // this removes some redundant codereturn null;}
three:
In design mode 2 we see, there are many duplicated code between Entity Beans and Struct, such as the same field declaration (corresponding to table columns in the database). If you let the Entity Bean inherited from the structure to avoid redundant code. But this design still does not display the contact between Beans.
Code snippet for Company Entity Beanpublic class CompanyBean extends CompanyStructimplements EntityBean {EntityContext entityContext; // all fields in CompanyStruct are available for CMPpublic Integer ejbCreate (CompanyStruct Struct) throws CreateException {this.comId = struct.comId; // set the primary keysetData (struct ); // this removes Some Redundant Codereturn NULL;}
The rest of the code is exactly the same as the implementation and design mode 2 of the getData () and setData () methods.
four:
In the design mode 3 we see that the BEAN is slightly shrunk after inheriting from the Struct, and all fields can be defined as a CMP field. Here, we can further correct the implementation of setData () and getData () to reduce the amount of code. We add a method for this Structure.
value Object code snippet for Companypublic class CompanyStruct implementsjava.io.Serializable {public Integer comId; public String comName; public String comDescription; public Timestamp mutationDate; public void copyFrom (CompanyStruct struct) {comId = struct.comId; comName = struct.comName; COMDESCRIPTION = Struct.ComDescription = struct.mutationdate;}} Since the entity bean is inherited from struct, the CopyFrom () method can be referenced in the implementation class of the bean. Of course, it must be noted that this CopyFrom () The method is not a business method, which does not need to be exposed to the caller in the remote interface of the bean. Now, getData () and setData () methods can simplify further simplification.
Code snippet for an entity bean's getdata () public companyStruct getData () {companyStruct result = new companyStruct (); result.copyfrom (this); return result;}
Here, THIS is incorporated into CopyFrom (). Since the enttity bean is inherited from the Struct, this Entitty Bean can be incorporated as a Struct. The EJB container does not agree to pass the THIS pointer as a parameter because an instance of accessing a bean simultaneously in both control threads may cause a transaction conflict. But in fact, we did not violate this principle because we did not deliver this reference between the beans and did not reference any ways that may cause a transaction conflict.
Code snippet for an entity bean's setdata () public void setdata {this.copyfrom (struct);}
This implementation method has the advantage of making the BEAN implementation of the category of the bean to achieve the code of the bean. This design pattern has greatly enhanced code and its streamline, readability and maintainability. Any database of any database only needs to modify the struct as the base class, and there is almost no need to modify the BEAN code. This change is separated from Struct, and the deployment descriptor needs to be modified when the CMP field changes. This makes better adaptation of design changes when developing. Here, there is still no relationship between the bean, which will be solved in the design mode 5. Fives:
Just as we see in the design mode 4, the implementation of the Entity Bean is reduced to only a few lines in Ejbcreate () and getData () and setdata () methods, regardless of the number of CMP fields. The next step is modeling The company and the employee's Entity Beans, this is a bit cumbersome and suggesting that the reader is known for the OR Mapping and advanced CMP of Borland's public class CompanyBean extends CompanyStructimplements EntityBean {EntityContext entityContext; // CMP for all fields in the CompanyStructpublic java.util.Collection employees; // one-to-many // rest of the code including getData () and setData () public java. Util.collection getemployees () {return Employees;}} The following is a program fragment of the employee Entity Bean: public class EmployeeBean extends EmployeeStructimplements EntityBean {EntityContext entityContext; // CMP for all fields in EmployeeStruct EXCEPT // the comIdpublic Company company; // remote reference to company} In the above program fragment, the employee Entity Bean is inherited from the employee structure, and the employee structure itself has a field comID representing the foreign key between employees and companies. In all previous design patterns, this field is CMP. And Design Mode 5 This field is removed from the CMP in the CMP in Deployment Descriptor. The remote reference to the company's Entity Bean is now CMP. How is the problem now updated in getData () and setData () methods The company's entry bean references, when these methods are only GET and SET COMID (not being cmp in the design mode context). Simply, the structure of the process has not changed and the field COMID (no longer cmp) is copied in the RPC to Entity bean and copy from the Entity Bean. It is required to update the remote reference to the company's Entity Bean in must be written to the database and read it from the database. We need to use the ejbload () and ejbstore () methods in the Entity Bean implementation class To do this for us. The code snippet of the EjbLoad () method in the employee Entity Bean is as follows: Public void ejbload () {try {comid = (company == null) NULL: (Integer) company.getPrimaryKey ();} catch (exception e) {// throw some runtime exception (E.g. Ejbexception)}}} The above code is almost no longer needed. When the data is read from the database (at the beginning of the transaction), the COMID (not a CMP) field is set in the employee Entity Bean. Therefore, when the getData () method is called, the returned structure will Contains the value of the correct COMID. The EJBStore () method in the Entity Bean is as follows: public void ejbStore () {try {company = (comId == null) null: beanGlossary.getCompanyHome () findByPrimaryKey (comId);} catch (Exception e) {// throw some runtime exception (e.g. EJBException)}?.} EJBSTORE () is called when the data is written when the data is written to the database. In this case, the value of the COMID is modified (by calling the setData method), this must be written to the database. The code in the above method COMID Transforming into a company's remote reference. (After all, comid is the primary key of the company's Entity Bean). The reason for using empty Check is that the database cannot save null values (weak references between tables), and these also need to model. No situation, Java's basic types of packages are better than using basic types, because they can save null values and easy to convert into other forms. The above BEANGLOSSARY class code snippet is easy to confuse. This is actually a Utility captured EJB's Lookup Class (a stateless session bean), in the case of Entity Bean and the status session bean, the Home interface LOOKUP is buffered. In the case of stateless session beans, the Remote interface is buffered (as EJB specification 1.1 Part part, a SLSB called Create () is not optimized in the HOME interface). By the above context buffer, we mean the first request is LOOKUP. Subsequent calls are already in object references initialization code fragment home interface or remote interface .BeanGlossarySB utility class as follows: public class BeanGlossarySB implements SessionBean {private context context = null; public javax.naming.Context getContext () throwsNamingException {if (context == null) context = new javax .naming.InitialContext (); return context;} // Companyprivate CompanyHome companyHome = null; public CompanyHome getCompanyHome () throwsNamingException {companyHome = ((CompanyHome) javax.rmi.PortableRemoteObject.narrow (getContext () lookup ( "java:. comp / re-information )j j..company"? )); return company;} // rest of the ejbs} In the design mode 5, we did not handle the HOME interface of the Entity Bean. In the case of employee entity beans, there will be a Finder element in a few lines of FindemPloyeesbyCompany (Company PCompany), which will return the collection of employee remote references. The DeploymentDescriptor Map in the company's Entity bean has a collection of Employees for the Finder element defined above. This way, the method in the company's entry bean getETemployees () in the Remote interface returns the remote referenced by the company Collection.