我是 EJB 技术的新手,所以最近开始学习 EJB3.0,在阅读书籍/在线资源时,我发现了很多与 EJB2.0 的参考和比较,以及 EJB3.0 如何通过要求开发人员创造热度来简化事情成分。我放弃了对 EJB3.0 的阅读,开始阅读 EJB2.0。我从“Richard Monson-Haefel III 版的企业 Java Beans”一书开始,它相当旧的版本(2000 年)但非常全面。在容器管理关系之前,我非常了解这些事情。我发现通过抽象持久性模型(抽象访问器方法)和抽象模式(部署描述符),在两个实体bean之间建立了关系。我还发现对于实体 bean 的持久性字段,在部署时,在虚拟持久字段和实际数据库表列之间建立映射。什么时候为虚拟关系字段建立这样的映射,是不是只在部署的时候? 单向一对一
<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>
在单向关系中,客户 EJB 具有关系字段 homeAddress。在数据库表 CUSTOMER 中有一个列 ADDRESS_ID。何时建立 homeAddress 和 ADDRESS_ID 之间的映射。
同样对于双向关系字段,其中两个关系角色都在部署描述符中定义了元素,但在实际数据库表中只有一个表保存外键(在多一和一可能的情况下),我们是否需要从其他表映射与任何列的关系角色?
双向一对一
<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 有关系字段 creditCard,而 CreditCard EJB 有关系字段 customer。在数据库中,CUSTOMER 表具有列 CREDITCARD_ID,而 CREDIT_CARD 表具有 CUSTOMER_ID 列。CUSTOMER 表是否需要有一个 CREDITCARD_ID 列。抽象模式是否总是需要精确映射到数据库模式?这种关系是在部署时建立的吗?