Tutorials > Program model > Extend the object model and modifying an existing task command

< Previous | Next >


Create the OrderGift entity bean

The OrderGift entity bean allows you to access the information in the XORDGIFT table. Within the entity bean we will create fields that map to the columns in the XORDGIFT table.


Procedure

  1. Open WebSphere Commerce Developer to the J2EE Perspective.

  2. Within the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsData > Deployment Descriptor: WebSphereCommerceServerExtensionsData.

  3. From the Deployment Descriptor's pop-up menu, select New > Enterprise Bean.

  4. In the Create an Enterprise Bean window:

    1. Select Entity bean with container-managed persistence (CMP) fields.

    2. From the EJB Project list, ensure WebSphereCommerceServerExtensionsData is selected.

    3. In the Bean name field, enter OrderGift

    4. In the Source folder field, leave the default value of ejbModule.

    5. In the Default package field, enter com.ibm.commerce.extension.objects.

      This is the package name WebSphere Commerce suggests for all new custom entity beans.

    6. From the CMP vlist, select 1.x.

    7. Click Next.

  5. In the CMP Attributes text box, add fields that correspond to columns in the XORDGIFT table:

    1. Select the default value id : Java.lang.Integer and click Remove.

    2. Click Add.

    3. Create the ordersId field, with the following information:

      Parameter name Parameter value
      Name ordersId
      Type java.lang.Long

      You must use the java.lang.Long data type, not the long data type.

      Key Field Select

    4. Click Apply.

    5. Create the receiptName field,...

      Parameter name Parameter value
      Name receiptName
      Type java.lang.String
      Access with getter and setter methods Select
      Promote getter and setter methods to remote interface Clear

    6. Click Apply. Clearing the promote getter and setter methods to remote interface causes less overhead and better performance. Since the getter and setter methods are not promoted to the remote interface, clients will not be able to access the methods. Instead of using the remote interface, CopyHelper Access Beans exist that contain getters and setters for CMP fields that do not exist on the remote interface. The methods within the CopyHelper Access Beans write data to a hash table that in turn updates the database when commitCopyHelper() is called. The hash table can record multiple database changes then update the database in one transaction, unlike the remote methods which require one transaction per change. A method called refreshCopyHelper() that does the reverse function of commitCopyHelper(). The method refreshCopyHelper() writes to the hash table from the database. Clearing the check box ensures that the hashtable is used, resulting in the performance increase.

    7. Create the senderName field,...

      Parameter name Parameter value
      Name senderName
      Type java.lang.String
      Access with getter and setter methods Select
      Promote getter and setter methods to remote interface Clear

    8. Click Apply.

    9. Create the msgField1 field,...

      Parameter Name Parameter Value
      Name msgField1
      Type java.lang.String
      Access with getter and setter methods Select
      Promote getter and setter methods to remote interface Clear

    10. Click Apply.

    11. Create the msgField2 field,...

      Parameter name Parameter value
      Name msgField2
      Type java.lang.String
      Access with getter and setter methods Select
      Promote getter and setter methods to remote interface Clear

    12. Click Apply.

    13. Create the optcounter field,...

      Parameter name Parameter value
      Name optcounter
      Type short
      Access with getter and setter methods Clear

    14. Click Apply then click Close.

    15. Ensure Use the single key attribute type for the key class is not selected, then click Next. Clearing this check box ensures that a key class is created, ensuring consistency with other beans.

  6. In the EJB Java Class Details window:

    1. To select the bean's superclass, click Browse. The Type Selection window opens.

    2. In the Select a class using: (any) field, enter ECEntityBean and click OK. This selects the com.ibm.commerce.base.objects.ECEntityBean as the superclass.

    3. Click Next.

    4. Deselect Add bean to Class Diagram.

    5. Click Finish.

  7. Set the isolation level for the OrderGift entity bean

    An entity bean's isolation level defines when updated data is accessible to other transactions.

    To set the isolation level for the OrderGift entity bean:

    1. Double-click Deployment Descriptor: WebSphereCommerceServerExtensionsData to open it for editing.

    2. Click the Access tab.

    3. Click Add next to the Isolation Level field.

    4. Select Read Committed, then click Next.

    5. Select the OrderGift bean, then click Next.

    6. Select OrderGift to select all of its methods, or click Apply to all.

    7. Click Finish.

    8. Save the work and keep the editor open.

  8. Set the security identity of the OrderGift entity bean

    You will specify that the OrderGift entity bean uses the security identity of the EJB container.

    To set the security identity of the bean:

    1. In the Deployment Descriptor editor, select the Access tab.

    2. Click Add next to the Security Identity (Method level) field.

    3. Select Use identity of EJB server, then click Next. Always use this setting for all new entity beans.

    4. Select the OrderGift bean, then click Next.

    5. Click Apply to all.

    6. Click Finish.

    7. Save the work and keep the deployment descriptor editor open.

  9. Set the security role of the OrderGift entity bean

    The security role of an entity bean determines which roles can execute the methods within the bean at runtime. You will specify the WCSecurityRole as the role that can execute the OrderGift entity bean's methods.

    To set the security role for the methods in the bean:

    1. In the Deployment Descriptor editor, select the Assembly tab.

    2. In the Method permissions section, click Add.

    3. Select Security Roles then WCSecurityRole and click Next.

    4. From the list of beans found, select OrderGift and click Next.

    5. In the Method elements page, click Apply to All, then click Finish.

    6. Save the work and close the deployment descriptor editor.

  10. Remove generated fields and methods

    In the next section, you remove the fields and methods related to the entity context that are generated by Rational Application Developer. These fields and methods are not required since the ECEntityBean base class provides its own implementation of these methods.

    To delete the generated entity context fields and methods:

    1. In the Enterprise Explorer view, expand the WebSphereCommerceServerExtensionsData project.

    2. Expand the Deployment Descriptor: WebSphereCommerceServerExtensionsData > Entity Beans > OrderGift then double-click OrderGiftBean.

    3. In the Outline view, delete the following generated fields and methods:

      1. myEntityCtx

      2. getEntityContext()

      3. setEntityContext(EntityContext)

      4. unsetEntityContext()

    4. Save the work.

  11. Set all gift message fields in the ejbCreate method

    Modify the generated ejbCreate method so that four gift message fields are explicitly set when a new OrderGift bean is created. You will set fields to null because you do not yet know their values.

    To set the fields to null:

    1. In the Outline view, select the ejbCreate(Long) method. Its source code displays...

      public com.ibm.commerce.extension.objects.OrderGiftKey 
         ejbCreate(java.lang.Long ordersId)
         throws javax.ejb.CreateException {
            _initLinks();
            this.ordersId = ordersId;
            return null;
      }
      

    2. Add the following line as the first line of the method after the {: this.initializeFields();

    3. Add the following lines after the this.ordersId = ordersId; line.

      this.senderName = null;
      this.receiptName = null;
      this.msgField1 = null;
      this.msgField2 = null;
      OrderGiftKey myNewOrderGiftKey = new OrderGiftKey(ordersId);
      this.initializeOptCounter(myNewOrderGiftKey);
      

      The resulting method displays...

      public com.ibm.commerce.extension.objects.OrderGiftKey
        ejbCreate(java.lang.Long ordersId)    throws javax.ejb.CreateException {      
       this.initializeFields();_initLinks();
      this.ordersId = ordersId;
      this.senderName = null;
      this.receiptName = null;
      this.msgField1 = null;
      this.msgField2 = null;
      OrderGiftKey myNewOrderGiftKey = new OrderGiftKey(ordersId);
      this.initializeOptCounter(myNewOrderGiftKey);
      return null; } 
      

    4. Save the changes.

  12. Create new ejbCreate and ejbPostCreate methods

    Create new ejbCreate and ejbPostCreate methods that take the orders ID, recipient name, sender name and message fields as parameters.

    To create the ejbCreate method:

    1. Create a new ejbCreate(Long, String, String, String, String) method, by adding the following code into the class:

      /*
      * Takes the ordersId and gift message as parameters and sets
      * the values of the fields as appropriate.
      * @param ordersId The unique identifier of the order with the gift message
      * @param receiptName Name of the gift recipient
      * @param  senderName Name of the person sending the gift
      * @param msgField1 First part of the message
      * @param msgField2 Second part of the message
      */
      public com.ibm.commerce.extension.objects.OrderGiftKey 
         ejbCreate(java.lang.Long ordersId,              
         java.lang.String receiptName,
         java.lang.String senderName,
         java.lang.String msgField1,
         java.lang.String msgField2)
         throws javax.ejb.CreateException {
            _initLinks();
            this.initializeFields();
            this.ordersId = ordersId;
            this.senderName = senderName;
            this.receiptName = receiptName;
            this.msgField1 = msgField1;
            this.msgField2 = msgField2; 
            this.initializeOptCounter(ordersId);
            return null;
        }
      

    2. Save the code changes.

      A warning might display because you do not have a matching ejbPostCreate method. The warning can be ignored as this will be done in next step.

    3. You must add this new ejbCreate method to the home interface to make the method available in the generated access bean. In the Outline view, from the ejbCreate(Long, String, String, String, String) method's pop-up menu, select Enterprise Bean > Promote to Home Interface.

    Next create a new ejbPostCreate(Long, String, String, String, String) method so that it has the same parameters as the ejbCreate(Long, String, String, String, String) method:

    1. Double-click the OrderGiftBean class to open it and view its source code.

    2. Create a new ejbPostCreate(Long ordersId, String receiptName, String senderName, String msgField1, String msgField2) method, by adding the following code into the class:

      public void ejbPostCreate(
         java.lang.Long ordersId,    java.lang.String receiptName,    java.lang.String senderName,    java.lang.String msgField1,    java.lang.String msgField2)
            throws javax.ejb.CreateException {
         }
      

    3. Save the code changes.

  13. Create a database and table definition

    1. If you have not completed the programming tutorial Tutorial: Creating new business logic, create a database definition. If you completed that tutorial then you already have a database definition and can skip this step and continue with step 13b.

      1. Right-click WebSphereCommerceServerExtensionsData and select New > Other.

      2. From the wizard, select Data > Physical Data Model and enter MyDBSchema as the file name.

      3. Select the appropriate database type for the development environment:

        • DB2 for Linux, UNIX, and Windows

        • Oracle

        The destination folder should populate after selecting the database type.

      4. Select the appropriate database version:

        • v9.5

        • v11

      5. Click Finish.

      6. In the Enterprise Explorer view, navigate to WebSphereCommerceServerExtensionsData > Data Models > MyDBSchema.dbm.

      7. Right-click Database > Add Data Object > Schema. In the properties panel, enter NULLID in the name field. You use NULLID as the schema name because this allows flexibility for EJB beans to work against any schema. If the schema name is filled in with a specific value other than NULLID, the EJB bean would only work for tables created using a specific schema.

    2. Right-click NULLID > Add Data Object > Table. Enter XORDGIFT as the table name.

    3. Add columns to the table by clicking the Columns tab and clicking the New symbol, just above the name column:

      1. Create the following columns:

        ORDERSID column properties and values
        Property Value
        Name ORDERSID
        Primary key Selected
        Data type

        • BIGINT

          If selected, the other properties cannot be modified by default.

        • Number

        Length

        38

        Scale

        0

        Not null Selected

        RECEIPTNAME column properties and values
        Property Value
        Name RECEIPTNAME
        Data type

        • VARCHAR

        • VARCHAR2

        Length 50

        SENDERNAME column properties and values
        Property Value
        Name SENDERNAME
        Data type

        • VARCHAR

        • VARCHAR2

        Length 50

        MSGFIELD1 column properties and values
        Property Value
        Name MSGFIELD1
        Data type

        • VARCHAR

        • VARCHAR2

        Length 50

        MSGFIELD2 column properties and values
        Property Value
        Name MSGFIELD2
        Data type

        • VARCHAR

        • VARCHAR2

        Length 50

        OPTCOUNTER column properties and values
        Property Value
        Name OPTCOUNTER
        Data type SMALLINT
        Not null Selected

  14. Map the XORDGIFT table to the OrderGift entity bean

    The next step is to map the XORDGIFT table to the OrderGift entity bean. Since the OrderGift bean and the XORDGIFT table already exist, Meet-in-the-middle mapping is used.

    1. If you have completed Tutorial: Creating new business logic, do the following to create the mapping:

      1. In the J2EE perspective, Enterprise Explorer view, highlight the EJB Projects > WebSphereCommerceServerExtensionsData > ejbModule > META-INF > backends > databaseType > Map.mapxmi file.

      2. From the Map.mapxmi file's pop-up menu, select Open With > Mapping Editor.

      3. When the Map.mapxmi file opens, in the Enterprise Beans pane, highlight the OrderGift bean. In the Tables pane, highlight the XORDGIFT table.

      4. Map the fields in the OrderGift bean to the columns in the XORDGIFT table by selecting Match By Name from the bean's pop-up menu.

      5. Save the Map.mapxmi file.

    2. If you have not completed Tutorial: Creating new business logic, do the following to create the mapping:

      1. In the J2EE perspective, right-click WebSphereCommerceServerExtensionsData and select Jave EE > EJB to RDB Mapping (1.x - 2.x) > Generate Map.

      2. On the first page of the Mapping wizard we will be asked to Specify the backend folder that will contain generated mapping file, database schema and other runtime information. The Use an existing backend folder radio button is pre-selected.

      3. Click Next.

      4. Select Meet In The Middle and click Next.

      5. Select None and click Finish. The Map.mapxmi editor opens.

      6. In the Enterprise Beans pane, highlight the OrderGift bean. In the Tables pane, highlight the XORDGIFT table.

      7. Map the fields in the OrderGift bean to the columns in the XORDGIFT table by right-clicking the OrderGift bean and selecting Match By Name from the bean's pop-up menu.

      8. Save the Map.mapxmi file.

  15. Enable optimistic locking

    To enable optimistic locking:

    1. Enable optimistic locking in the deployment descriptor:

      1. In the EJB Deployment Descriptor in the Bean tab select the OrderGift entity bean.

      2. Scroll down to the Concurrency Control section.

      3. Select Enable optimistic locking.

      4. Save the changes.

    2. Set the optimistic predicate value of the optcounter field:

      1. Double-click WebSphereCommerceServerExtensionsData > ejbModule > META-INF > backends > databaseType > Map.mapxmi.

      2. Map.mapxmi should open in the Mapping Editor.

      3. In the Overview section, in the Enterprise Beans pane, select optcounter : short.

      4. Select the Properties view, and ensure true is selected from the OptimisticPredicate list. Your screen should resemble the following screen capture:

  16. Create an access bean

    Once the OrderGiftBean entity has been created and the schema is correctly mapped, you can create a Copy Helper access bean for the entity bean. This access bean makes it simpler for applications to access information contained in the OrderGift entity bean. The methods within the Copy Helper access bean write data to a hash table that in turn updates the database when commitCopyHelper() is called. The hash table can record multiple database changes then update the database in one transaction, unlike the remote methods which require one transaction per change. A method called refreshCopyHelper() that does the reverse function of commitCopyHelper(). The method refreshCopyHelper() writes to the has table from the database. Deselecting the box ensures that the hashtable is used, resulting in the performance increase.

    To create the Copy Helper access bean for the OrderGift entity bean:

    1. Open the deployment descriptor.

    2. In the Bean tab, select OrderGift.

    3. In the bottom right pane, scroll down to the Access Beans section and click Add.

    4. Select Copy Helper and click Next.

    5. Select the OrderGift bean and click Next.

    6. From the Constructor method drop-down list, select findByPrimaryKey(com.ibm.commerce.extension.objects.OrderGiftKey).

    7. Select all attributes in the Attribute Helpers section.

    8. Click Finish.

    9. Save the changes.

    You can view the newly generated access bean code by switching to the Project Navigator view, expand the WebSphereCommerceServerExtensionsData project, and expand ejbModule sub-folder, then expand com.ibm.commerce.extension.objects. A new class called OrderGiftAccessBean and a new interface called OrderGiftAccessBeanData are created and display inside the package.

  17. Generate the deployed code

    The code generation utility analyzes the beans to ensure that Sun Microsystems' EJB specifications are met and it ensures that rules specific to the EJB server are followed. In addition, for each selected enterprise bean, the code-generation tool generates the home and EJBObject (remote) implementations and implementation classes for the home and remote interfaces, as well as the JDBC persister and finder classes for CMP beans. It also generates the Java ORB, stubs, and tie classes required for RMI access over IIOP, as well as stubs for the home and remote interfaces.

    To generate the deployed code:

    1. Navigate to EJB Projects > WebSphereCommerceServerExtensionsData.

    2. Open Deployment Descriptor WebSphereCommerceServerExtensionsData.

    3. In the Overview tab, scroll down to the JNDI - CMP Connection Factory Binding section and in the JNDI name field enter the value that matches the database type:

      • jdbc/WebSphere Commerce Cloudscape DataSource demo

      • jdbc/WebSphere Commerce DB2 DataSource demo

      • jdbc/WebSphere Commerce Oracle DataSource demo

    4. Save and close the Deployment Descriptor.

    5. Right-click WebSphereCommerceServerExtensionsData and select Java EE > Prepare for Deployment.

< Previous | Next >


+

Search Tips   |   Advanced Search