EJB design Mode 5

Source: Internet
Author: User

As we see in design mode 4, the implementation size of the Entity bean is reduced to just a few lines in the Ejbcreate (), GetData () and SetData () methods, regardless of the number of CMP fields. The next step is modeling the company and employees ' Entity Beans, this is a bit cumbersome and advises readers to first understand Borland's <EJB Programmer's Guide > or Mapping and Advanced CMP.

Modeling the relationship does not require code changes to the structure at all, but the entity beans implementation class needs a little modification to reflect the relationship between the two entities, given the deployment descriptor need minor modifications.

As before, the Entity Bean inherits from the structure, and the following is a code fragment of the company Entity Bean:

  public class CompanyBean extends CompanyStruct
   implements EntityBean {
   EntityContext entityContext;
   // CMP for all fields in the CompanyStruct
   public 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 for an employee entity bean:

  public class EmployeeBean extends EmployeeStruct
   implements EntityBean {
   EntityContext entityContext;
   //CMP for all fields in EmployeeStruct EXCEPT
   //the comId
   public Company company;//remote reference to company
   }

In the above program fragment, the employee entity Bean inherits from the employee structure, and the employee structure itself has a field comid representing the foreign key between the employee and the company, which is CMP in all previous design patterns. In design mode 5, this field is used in the deployment The Un-checking method in descriptor is removed from CMP. The remote reference to the corporate entity bean is now CMP. The question now is how to update the company SetData Bean Reference in the GetData () and Entity () methods. The values of these methods are only get and set Comid (not CMP in design mode context). Simply put, the structure of the process is unchanged and the field comid (no longer CMP) is copied to the entity bean in RPC and from the entity Bean copy out. What is needed is a remote reference to the corporate entity bean that is updated when it must be written to the database and read out of the database. We need to do this for us in the entity Bean implementation class with the Ejbload () and Ejbstore () methods.

The code snippet for 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 requires little explanation. When the data is read from the database (at the beginning of the transaction), the Comid (not CMP) field is set in the employee entity bean. So when the GetData () method is called, The returned structure will contain values that are correctly comid. The Ejbstore () method in the employee entity Bean is as follows:

  public void ejbStore() {
   try {
   company = (comId ==
   null)?null:beanGlossary.getCompanyHome().findByPrimary
   Key(comId);
   } catch (Exception e) {
   //throw some runtime exception (e.g. EJBException)
   }
   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.