문제

I am a total novice to EJB technologies, so recently I started learning EJB3.0, when reading through books/online resources, I found lot of references and comparisons to EJB2.0 and how EJB3.0 simplifies things by requiring developer to create fever components. I left my reading of EJB3.0 and started reading EJB2.0. I started with book "Enterprise Java Beans by Richard Monson-Haefel III Edition", its pretty old edition (2000) but is very comprehensive. I understood the things pretty well until container managed relationship. I found that through abstract persistence model (abstract accessor methods) and abstract schema (deployment descriptor), a relationship is established between two entity beans. Also I found that for persistence fields of entity bean, at the time of deployment a mapping is established between virtual persistent field and actual database table column. When do such mapping is established for virtual relationship field, is it at the time of deployment only? Uni-directional one to one

    <ejb-jar>
<enterprise-beans>
    <entity>
        <ejb-name>CustomerEJB</ejb-name>
        <home>com.titan.customer.CustomerHomeRemote</home>
        <remote>com.titan.customer.CustomerRemote</remote>
        <ejb-class>com.titan.customer.CustomerBean</ejb-class>
        <persistence-type>Container</persistence-type>
        <prim-key-class>java.lang.Integer</prim-key-class>
        <reentrant>False</reentrant>
        <cmp-version>2.x</cmp-version>
        <abstract-schema-name>Customer</abstract-schema-name>
        <cmp-field><field-name>id</field-name></cmp-field>
        <cmp-field><field-name>lastName</field-name></cmp-field>
        <cmp-field><field-name>firstName</field-name></cmp-field>
        <primkey-field>id</primkey-field>
        <security-identity><use-caller-identity/></security-identity>
    </entity>
    <entity>
        <ejb-name>AddressEJB</ejb-name>
        <local-home>com.titan.address.AddressHomeLocal</local-home>
        <local>com.titan.address.AddressLocal</local>
        <ejb-class>com.titan.address.AddressBean</ejb-class>
        <persistence-type>Container</persistence-type>
        <prim-key-class>java.lang.Integer</prim-key-class>
        <reentrant>False</reentrant>
        <cmp-version>2.x</cmp-version>
        <abstract-schema-name>Address</abstract-schema-name>
        <cmp-field><field-name>id</field-name></cmp-field>
        <cmp-field><field-name>street</field-name></cmp-field>
        <cmp-field><field-name>city</field-name></cmp-field>
        <cmp-field><field-name>state</field-name></cmp-field>
        <cmp-field><field-name>zip</field-name></cmp-field>
        <primkey-field>id</primkey-field>
        <security-identity><use-caller-identity/></security-identity>
    </entity>
</enterprise-beans>
<relationships>
    <ejb-relation>
        <ejb-relation-name>Customer-Address</ejb-relation-name>
        <ejb-relationship-role>
            <ejb-relationship-role-name>Customer-has-an-Address</ejb-relationship-role-name>
            <multiplicity>One</multiplicity>
            <relationship-role-source>
                <ejb-name>CustomerEJB</ejb-name>
            </relationship-role-source>
            <cmr-field>
                <cmr-field-name>homeAddress</cmr-field-name>
            </cmr-field>
        </ejb-relationship-role>
        <ejb-relationship-role>
            <ejb-relationship-role-name>Address-belongs-to-Customer</ejb-relationship-role-name>
            <multiplicity>One</multiplicity>
            <relationship-role-source>
                <ejb-name>AddressEJB</ejb-name>
            </relationship-role-source>
        </ejb-relationship-role>
        </ejb-relation>
</relationships>
</ejb-jar>

In Unidirectional relationship Customer EJB has relationship field homeAddress. In the database table CUSTOMER has a column ADDRESS_ID. When do the mapping between homeAddress & ADDRESS_ID is established.

Also for bi-directional relationship fields where both relationship roles have element defined in deployement descriptor but in the actual database table only one table holds the foreign key (in case of many-one and one-may), do we need to map from other relationship role to any column?

Bi-directional one-to-one

<relationships>
    <ejb-relation>
    <ejb-relation-name>Customer-CreditCard</ejb-relation-name>
    <ejb-relationship-role>
        <ejb-relationship-role-name>Customer-has-a-CreditCard</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
            <ejb-name>CustomerEJB</ejb-name>
        </relationship-role-source>
        <cmr-field>
        <cmr-field-name>creditCard</cmr-field-name>
        </cmr-field>
    </ejb-relationship-role>
    <ejb-relationship-role>
        <ejb-relationship-role-name>CreditCard-belongs-to-Customer</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
            <ejb-name>CreditCardEJB</ejb-name>
        </relationship-role-source>
        <cmr-field>
        <cmr-field-name>customer</cmr-field-name>
        </cmr-field>
    </ejb-relationship-role>
    </ejb-relation>
</relationships>

Customer EJB has relationship field creditCard, While CreditCard EJB has relationship field customer. In database CUSTOMER table has column CREDITCARD_ID while CREDIT_CARD table has column CUSTOMER_ID. Is it required for CUSTOMER table to have a CREDITCARD_ID column. Is it always necessary for abstract schema to map exactly to the database schema? Is this relationship established at deployment time?

도움이 되었습니까?

해결책

Yes, the mapping from CMP metadata to DB tables happens at deploy time using vendor-specific tools. From the EJB 3.0 specification:

The EJB deployment descriptor for EJB 2.1 entity beans describes logical relationships among entity beans. It does not provide a mechanism for specifying how the abstract persistence schema of an entity bean or of a set of interrelated entity beans is to be mapped to an underlying database. This is the responsibility of the Deployer, who, using the Container Provider’s tools, uses the logical relationships that are specified in the deployment descriptor to map to the physical relationships that are specific to the underlying resource.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top