当前页面: 开发资料首页 → Netbeans 专题 → CMP Mapping in NetBeans IDE 4.1
摘要: Mapping is the process of tying a relational model of data, usually the schema of a relational database, to an object-oriented model. When mapping beans in the IDE, you are tying the database schema to a set of interrelated container-managed persistence (CMP) entity beans. These CMP beans have fields that the container uses to map to fields in the database.
When mapping entity beans, the IDE uses the database schema, which is simply an xml file with metadata describing the database and relationships in the database but which does not contain any database data. If you do not have a schema, you can use the IDE to capture the schema from a database.
The IDE can simplify the process of creating CMP entity beans and mapping entity beans to a database in the following ways:
After the beans are mapped, you can modify the mappings in the IDE if the fields or relationships in the database change.
This article covers the following information about using Netbeans 4.1 and mapping CMP entity beans on the Sun Java System Application Server 8.1.
This article is assuming you are familiar with CMP entity beans and the Netbeans IDE. For more information on creating CMP entity beans, see the container-managed persistence chapter in the J2EE 1.4 Tutorial for NetBeans IDE 4.1 and the NetBeans IDE 4.1 Quick Start Guide for J2EE Applications.
Implementation for entity beans that use CMP is mostly a matter of mapping CMP fields and CMR fields (relationships) to the database. When deploying an application to the Sun Java System Application Server, CMP bean mapping information is contained in the sun-cmp-mappings.xml file, which the IDE generates when the application is deployed to the Sun Java System Application Server.
The Sun Configuration visual editor in the IDE enables you to edit the sun-cmp-mappings.xml file. You can map the following in the Sun Configuration editor:
When mapping entity beans, each bean is mapped to a table within a single database schema, and all related beans must also map to that schema. The data for mapping the database schema to the project entity beans is contained in the sun-cmp-mappings.xml file.
If a database or database schema are available, the easiest way to generate CMP mapping for a bean is by using the New CMP Entity Beans from Database wizard.
When you click Finish in the wizard, the IDE does the following:
If a database is available but you do not want to create new entity beans, you can map your entity beans to the database in the Sun Configuration editor. When you map the beans in the Sun Configuration editor, you specify the schema and the primary table for each bean. Each bean is mapped to a table within a single database schema. All related beans must also map to that schema.
The IDE generates the sun-cmp-mappings.xml file when you save your changes.
During development, it is possible that access to a database schema or a database is unavailable. In this case, you can create your CMP entity beans in the IDE and then create bean fields and relationships. The IDE can then generate the required tables when you deploy your application. The tables can be created on the database server which is bundled with the Sun Java System Application Server. After the tables are generated you can capture the schema and use this schema for mapping your beans.
Upon deployment, the IDE generates the tables for your beans. The relationships between the tables are generated based upon the relationships between your CMP beans. You can see the created tables under the Tables node for your database connection in the Runtime window. You can now capture the schema and map your existing beans, or you can create a new project and create new beans from the database or the database schema, in which case the IDE maps the entity beans for you.
After you have mapped your entity beans to the database, you can see the mappings in the Sun Configuration editor by clicking the sun-cmp-mappings.xml file in the Projects window. In the Sun Configuration editor, select the bean in the tree view in the left pane. The mappings for that bean are visible in the CMP Mappings tab. You can edit the bean mappings in the CMP Mapping tab.
After your entity beans are mapped to the database, you can edit the mappings in the IDE by editing the sun-cmp-mappings.xml file. You may need to manually map CMP entity beans in the following situations:
You can edit the sun-cmp-mappings.xml file by editing the XML in the Source Editor or using the Sun Configuration visual editor. The Sun Configuration wizards can help you when modifying field or relationship mappings. To edit the CMP mappings, select the node for the enterprise bean you wish to edit and then select the CMP Mapping tab. The CMP mappings are displayed in the Field Mappings table.
NOTE: If you are using a mapping file (sun-cmp-mappings.xml) without capturing the database meta data (a schema file), your mapping will not be shown in the Field Mappings table. However, this mapping file is still valid and deployable. You can continue to use this mapping file.
You can do the following in the CMP Mapping tab:
When you map CMP beans to a database, most beans map directly to tables, and the CMP fields map to columns in either the primary or a secondary table. Once a bean is mapped to a primary table, you have the option of mapping one or more secondary tables in the Advanced Settings dialog box.
A primary table is the table that most closely matches the bean you are mapping. A primary table must be selected for each CMP bean. The available primary tables are listed in the combo box. If no primary table is available in the combo box, you need to specify the database schema by clicking Advanced Settings. In the Advanced Settings dialog box, choose the schema from the Database Schema combo box, which lists the database schema files (.dbschema) in the project. If a schema is already specified, choosing a new schema removes all of the current field mappings that refer to this schema.
A secondary table enables you to map columns that are not part of your primary table directly to fields in your bean. You might want to do this if, for example, the data that represents a logical object is split across two or more tables.
A secondary table differs from relationships. You use secondary tables when you want columns from another table to be fields in the same bean. You use relationship fields when there is a logical relationship between tables. For example, if you want the entire row of another table to be an object in a single bean, you would map that with a relationship.
Setting secondary tables:
You can set secondary tables by clicking Advanced Settings in the CMP Mapping tab and
clicking Add to open the Mapped Secondary Table Setup dialog box.
If you want to map to columns from tables other than the primary table, you must specify one or more secondary tables. A secondary table must be related to the primary table by one or more columns whose associated rows have the same values in both tables. This link is called a reference key. Normally, this key is a foreign key in the primary table.
In the Field Mappings section, you can map a field to a column by selecting the column in the combo box for that field. The Field Mappings section has the following columns:
Additionally, the following mapping options are available in the CMP Mapping tab:
Choosing <map multiple columns> in the Mapped Column combo box opens the Map Field to Multiple Columns dialog box.
Mapping a field to more than one column:
When you map more than one field to any of these columns, the mappings can either completely overlap or not overlap at all. They cannot partially overlap. Consider the following three examples:
An entity bean may be related to other entity beans, and with container-managed persistence, the EJB container takes care of these relationships for you. For example, the cmporder container-managed persistence example in the J2EE 1.4 Tutorial for NetBeans IDE 4.1 contains entity beans that have self-referential relationships, one-to-one relationships, unidirectional relationships, unknown primary keys, primitive primary key types, and composite primary keys. It has entity beans that represent orders, parts, vendors, and line items, and each order may have one or more elements.
For the cmporder example to work correctly, the relationships between the entity beans representing these database tables must be configured correctly. When you generate CMP entity beans from an existing database, the IDE automatically configures the module's CMP relationships and CMP fields and creates a default mapping used by the Sun Java System Application Server. However, you can also modify the mappings manually in the Field Mappings table.
Mapping a relationship:
When you have foreign-key relationships between database tables,
you will usually want to preserve those relationships in the CMP beans mapped to those tables.
The Map Relationship Field wizard lets you determine how relationships between mapped CMP
beans are stored in the database. Relationships enable navigation from one bean to another,
in the same way that a foreign key enables navigation from one table to another.
The Map Relationship Field wizard takes you through the following steps:
Map to Key
This panel shows the following mapping information:
The editor will attempt to determine the most logical key column pairs between the two related beans based on existing foreign keys. If there are no foreign keys, you need to create the key column pairs by selecting local and foreign columns.
If you are linking the mapped tables using a join table, you map the relationship field first to a key in the join table in the Local to Join panel, and then to the foreign key in the Join to Foreign panel. All the tables, including the join table, must be contained in the same schema.
Local to Join
In this panel, you choose a join table, then map the relationship field to a key.
This is the relationship between the join table and the table the bean (MyBean) is mapped to.
If you do not have a join table, go back to the previous panel and select Link the Mapped Tables Directly.
Choose a join table that sits between the two tables that your beans are mapped to. The IDE will attempt to determine the most logical key column pairs between the join table and the table that MyBean is mapped to.
If the tables have a foreign key between them, the IDE will use the foreign key as the default key column pair. If there is no foreign key, then you must create a key by choosing a pair of columns that will allow navigation from the join table to the table to which MyBean is mapped. The columns in each pair are expected to have the same value.
You can create a compound key by clicking Add Pair to add additional Key Column Pairs. If the Next button is disabled, you need to pick a join table and a key column pair.
Join to Foreign Key
In this panel, you relate a second bean to the join table you chose in the previous panel.
The IDE will attempt to determine the most logical key column pairs between the join table and the table that the related bean is mapped to.
If the tables have a foreign key between them, the IDE will use the foreign key as the default key column pair. If there is no foreign key, then you must create a key by choosing a pair of columns that will allow navigation from the join table to the table to which the Related Bean is mapped. The columns in each pair are expected to have the same value.
You can create a compound key by clicking Add Pair to add additional Key Column Pairs. If the Finish button is disabled, you need to choose a join table and a key column pair.
After you have mapped your relationships, the Field Mappings field in the CMP Mapping tab displays <mapped relationship> in the Mapped Column field for the persistent field.
When you create or modify field mappings in the Sun Configuration editor, the IDE makes these changes in the sun-cmp-mappings.xml file. You can also edit this in the Source Editor by right-clicking the file and choosing Edit from the contextual menu.
The modified sun-cmp-mappings.xml file is copied to the SJS Application Server when the application is deployed.