Declaring the Deployment Descriptors

EJB QL enables you to traverse entity bean relationships. In this section, we discuss how to declare collection members in EJB QL and then present the full listing of the deployment descriptors.

Collection Member Declarations

In a one-to-many or a many-to-many relationship, the many side of the relationship consists of a collection of entity beans. You can declare a collection of values, in the FROM clause, using a collection member declaration. An identification variable of a collection member declaration is declared using the IN operator. For example, a finder method query to find all orders with pending line items can be written as follows:

SELECT DISTINCT OBJECT(o)
FROM Order AS o, IN(o.lineItems) as l
WHERE l.shipped = FALSE

In the preceding example, lineItems represents a collection of instances. In the FROM clause declaration IN(o.lineItems) l, the identification variable l evaluates to any LineItem value directly reachable from Order. This query navigates over the cmr-field lineItems (of the abstract schema type Order) to find line items, and uses the cmp-field shipped (of lineItem) to select those orders that have at least one line item that has not yet shipped. Note that this query does not select orders that have no line items. To find all orders with no associated line items, you can use the following query:

SELECT DISTINCT OBJECT(o)
FROM Order AS o
WHERE o.lineItems IS EMPTY

You can test whether a particular value is a member of the collection by using the MEMBER OF comparison operator as follows:

SELECT OBJECT(l)
FROM ORDER o, LineItem l
WHERE l MEMBER of o.lineItems

The preceding query finds all the line items that are attached to orders. In other words, it does not select line items that are not attached to any order.

Declaring the Standard Deployment Descriptor ejb-jar.xml

Now let's examine the ejb-jar.xml deployment descriptor shown in Listing 12.10. The ejb-jar.xml declares OrderEJB as the name of the order entity bean and specifies the home interface, remote interface, local home interface, local interface, and bean class. It declares the Order abstract persistent schema and declares that orderId is the primary key using the primkey-field element. Similarly, the deployment descriptor declares OrderLineItemEJB as the name of the order line item entity bean and specifies the home interface, remote interface, local home interface, local interface, and bean class. It declares the Order abstract persistent schema and declares that orderId is the primary key using the primkey-field element.

Listing 12.10. The Full Text of day12/ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC
'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
  <enterprise-beans>
    <entity>
      <ejb-name>OrderEJB</ejb-name>
      <home>day12.OrderHome</home>
      <remote>day12.Order</remote>
      <local-home>day12.OrderLocalHome</local-home>
      <local>day12.OrderLocal</local>
      <ejb-class>day12.OrderEJB</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>Order</abstract-schema-name>
      <cmp-field>
        <field-name>orderId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>studentId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>orderDate</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>status</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>amount</field-name>
      </cmp-field>
      <primkey-field>orderId</primkey-field>
    </entity>
    <entity>
      <ejb-name>OrderLineItemEJB</ejb-name>
      <local-home>day12.OrderLineItemLocalHome</local-home>
      <local>day12.OrderLineItemLocal</local>
      <ejb-class>day12.OrderLineItemEJB</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>OrderLineitem</abstract-schema-name>
      <cmp-field>
        <field-name>orderLineItemId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>courseId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>fee</field-name>
      </cmp-field>
      <primkey-field>orderLineItemId</primkey-field>
    </entity>
  </enterprise-beans>
  <relationships>
    <ejb-relation>
      <ejb-relation-name>Order-LineItems</ejb-relation-name>
        <ejb-relationship-role>
<ejb-relationship-role-name>Order-has-lineitems</ejb-relationship-role-name>
          <multiplicity>One</multiplicity>
          <relationship-role-source>
            <ejb-name>OrderEJB</ejb-name>
          </relationship-role-source>
          <cmr-field>
            <cmr-field-name>lineItems</cmr-field-name>
            <cmr-field-type>java.util.Collection</cmr-field-type>
          </cmr-field>
        </ejb-relationship-role>
        <ejb-relationship-role>
<ejb-relationship-role-name>lineItem-belongsto-Order </ejb-relationship-role-name>
          <multiplicity>Many</multiplicity>
          <cascade-delete/>
          <relationship-role-source>
            <ejb-name>OrderLineItemEJB</ejb-name>
          </relationship-role-source>
          <cmr-field>
            <cmr-field-name>order</cmr-field-name>
          </cmr-field>
        </ejb-relationship-role>
      </ejb-relation>
  </relationships>
</ejb-jar>

The deployment descriptor also declares a relationship using ejb-relation. The deployment descriptor declares the Order-LineItems relation using ejb-relation-name. The order side of the relationship declares the Order-has-lineitems role name with cardinality of One. It also declares OrderEJB as the EJB name using the ejb-name element in the relationship-role-source element. It specifies the container-managed relationship field lineItems using the cmp-field-name element in the cmp-field element. Similarly, the deployment descriptor also contains the line items' side of the relationship.

Because a line item object should exist only when the parent order exists, you want to ensure that when you delete an order, you also cascade delete all the line items that belong to the order. This is declared using the cascade-delete element on the line item side of the relationship.

Note

The cascade-delete element is used within a particular relationship to specify that the lifetime of one or more entity beans is dependent on the lifetime of another entity object. cascade-delete can be specified only for one-to-one and one-to-many relationships.

Let's examine how this works with an example. As shown in Figure 12.6, entity instances a and b are related. Instance b is composed of instance c and instance d is composed of c. Assume that the cascade-delete option was specified for composition relationships. When the remove method is called on an entity bean instance b, the EJB container performs the following:

1.
Calls the ejbRemove() method on instance b.

2.
Removes entity object b from all relationships, with instance a and instance c, in which it participates.

3.
Removes entity object b from the persistent store.

4.
If the cascade-delete element is specified for a related entity bean, removal is cascaded and any related entity bean instances are also removed. So, the container removes instance c, which in turn triggers the removal of instance d.

Figure 12.6. Example of cascade-delete.



Declaring the Vendor-Specific Deployment Descriptors

In this section, we'll examine the additional deployment descriptors that are specific to WebLogic Server and JBoss. Each server needs a couple of deployment descriptors.

In the case of WebLogic Server, you need to write the deployment descriptors weblogic-ejb-jar.xml and weblogic-cmp-rdbms-jar.xml. Listing 12.11 shows the weblogic-ejb-jar.xml deployment. It specifies the JNDI names of the OrderEJB entity bean's remote home interface using the jndi-name element and local home interface using local-jndi-name. The type-storage element specifies the full path (META-INF/weblogic-cmp-rdbms-jar.xml) of the file that stores the data of this persistence type. This file is stored in the META-INF subdirectory of the JAR file. Similarly, the elements local-jndi-name and so on are specified for the OrderLineItemEJB entity bean.

Listing 12.11. The Full Text of day12/weblogic-ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC
'-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN'
'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
  <weblogic-enterprise-bean>
    <ejb-name>OrderEJB</ejb-name>
    <entity-descriptor>
      <persistence>
        <persistence-use>
          <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
          <type-version>6.0</type-version>
          <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
        </persistence-use>
      </persistence>
    </entity-descriptor>
    <jndi-name>day12/Order</jndi-name>
    <local-jndi-name>day12/OrderLocal</local-jndi-name>
  </weblogic-enterprise-bean>
  <weblogic-enterprise-bean>
    <ejb-name>OrderLineItemEJB</ejb-name>
    <entity-descriptor>
      <persistence>
        <persistence-use>
          <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
          <type-version>6.0</type-version>
          <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
        </persistence-use>
      </persistence>
    </entity-descriptor>
    <local-jndi-name>day12/OrderLineItemLocal</local-jndi-name>
  </weblogic-enterprise-bean>
</weblogic-ejb-jar>

Listing 12.12 shows the weblogic-cmp-rdbms-jar.xml. It contains the WebLogic Server–specific deployment descriptors that define container-managed persistence services. It specifies the abstract-schema-to-database-element mapping for both OrderEJB and OrderLineItemEJB (the mapping for OrderLineItemEJB is not shown in the listing). This file also specifies the relationship that is managed by WebLogic using the weblogic-rdbms-relation element. This element specifies the mapping from the order_id foreign key column in OrderLineItemEJB to the order_id primary key column of OrderEJB.

Listing 12.12. The Text of day12/weblogic-cmp-rdbms-jar.xml
<!DOCTYPE weblogic-rdbms-jar PUBLIC
 '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB RDBMS Persistence//EN'
 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd'>
<weblogic-rdbms-jar>
  <weblogic-rdbms-bean>
    <ejb-name>OrderEJB</ejb-name>
    <data-source-name>jdbc/styejbDB</data-source-name>
    <table-map>
    <table-name>orders</table-name>
    <field-map>
      <cmp-field>orderId</cmp-field>
      <dbms-column>order_id</dbms-column>
    </field-map>
    <field-map>
      <cmp-field>studentId</cmp-field>
      <dbms-column>student_id</dbms-column>
    </field-map>
    <field-map>
      <cmp-field>orderDate</cmp-field>
      <dbms-column>order_date</dbms-column>
    </field-map>
    <field-map>
      <cmp-field>status</cmp-field>
      <dbms-column>status</dbms-column>
    </field-map>
    <field-map>
      <cmp-field>amount</cmp-field>
      <dbms-column>amount</dbms-column>
    </field-map>
   </table-map>
  </weblogic-rdbms-bean>
  <weblogic-rdbms-bean>
    <ejb-name>OrderLineItemEJB</ejb-name>
    . . .
  </weblogic-rdbms-bean>
  <weblogic-rdbms-relation>
    <relation-name>Order-LineItems</relation-name>
      <weblogic-relationship-role>
        <relationship-role-name>
             lineItem-belongsto-Order
        </relationship-role-name>
        <relationship-role-map>
        <column-map>
            <foreign-key-column>order_id</foreign-key-column>
            <key-column> order_id </key-column>
        </column-map>
        </relationship-role-map>
        <db-cascade-delete/>
      </weblogic-relationship-role>
  </weblogic-rdbms-relation>
</weblogic-rdbms-jar>

In the case of JBoss, you need to write the deployment descriptors jboss.xml and jbosscmp-jdbc.xml. Listing 12.13 shows the deployment descriptor jboss.xml. It specifies the JNDI names of the OrderEJB entity beanís remote home interface using jndi-name element, and local home interface using local-jndi-name. Also, it specifies the JNDI names of the OrderLineItemEJB entity bean's remote home interface using jndi-name.

Listing 12.13. The Full Text of day12/jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
 <jboss>
  <enterprise-beans>
    <entity>
      <ejb-name>OrderEJB</ejb-name>
      <jndi-name>day12/Order</jndi-name>
      <local-jndi-name>day12/OrderLocal</local-jndi-name>
    </entity>
    <entity>
      <ejb-name>OrderLineItemEJB</ejb-name>
      <local-jndi-name>day12/OrderLineItemLocal</local-jndi-name>
    </entity>
  </enterprise-beans>
</jboss>

Listing 12.14 shows the jbosscmp-jdbc.xml file. It contains the abstract-schema-to-database-element mapping for both OrderEJB and OrderLineItemEJB (the mapping for OrderLineItemEJB is not shown in the listing). This file also specifies the relationship that is managed by JBoss using the ejb-relation element. This element specifies the mapping from the order_id foreign key column in OrderLineItemEJB to the order_id primary key column of OrderEJB.

Listing 12.14. The Full Text of day12/jbosscmp-jdbc.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC
   "-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN"
   "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_0.dtd">
<jbosscmp-jdbc>
   <defaults>
      <datasource>java:/DefaultDS</datasource>
      <datasource-mapping>Hypersonic SQL</datasource-mapping>
      <create-table>false</create-table>
      <remove-table>false</remove-table>
      <pk-constraint>true</pk-constraint>
      <preferred-relation-mapping>foreign-key</preferred-relation-mapping>
   </defaults>
   <enterprise-beans>
      <entity>
         <ejb-name>OrderEJB</ejb-name>
         <table-name>orders</table-name>
         <cmp-field>
            <field-name>orderId</field-name>
            <column-name>order_id</column-name>
         </cmp-field>
         <cmp-field>
            <field-name>studentId</field-name>
            <column-name>student_id</column-name>
         </cmp-field>
         <cmp-field>
            <field-name>orderDate</field-name>
            <column-name>order_date</column-name>
         </cmp-field>
         <cmp-field>
            <field-name>status</field-name>
            <column-name>status</column-name>
         </cmp-field>
         <cmp-field>
            <field-name>amount</field-name>
            <column-name>amount</column-name>
         </cmp-field>
      </entity>
      <entity>
         <ejb-name>OrderLineItemEJB</ejb-name>
         .
							.
							.
   </enterprise-beans>
   <relationships>
      <ejb-relation>
         <ejb-relation-name>Order-LineItems</ejb-relation-name>
         <foreign-key-mapping/>
         <ejb-relationship-role>
<ejb-relationship-role-name>Order-has-lineitems</ejb-relationship-role-name>
            <key-fields>
               <key-field>
                  <field-name>orderId</field-name>
                  <column-name>order_id</column-name>
               </key-field>
            </key-fields>
         </ejb-relationship-role>
         <ejb-relationship-role>
<ejb-relationship-role-name>lineItem-belongsto-Order </ejb-relationship-role-name>
            <key-fields/>
         </ejb-relationship-role>
      </ejb-relation>
   </relationships>
</jbosscmp-jdbc>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116