站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题CMP Mapping in NetBeans IDE 4.1

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.

CMP Mapping on the Sun Java System Application Server

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:

Mapping Entity Beans to Tables

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.

Generating new CMP entity beans from a database or schema

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.

  1. In the Projects window, right-click the EJB module's node and choose New > CMP Entity Beans from Database.
  2. Select the type and database source from the combo box. Choose JDBC Connection if you have already set up a JDBC connection or you can connect to the database and want to set up the connection now. Choose Database Schema if you have already captured a database schema.
  3. Type the package name for your entity beans, specify if you want the IDE to generate the finder methods and click Next. The wizard shows you all of the tables in the selected database.
  4. Select the tables you need, click Add, then click Finish.

When you click Finish in the wizard, the IDE does the following:

  • Creates the CMP bean for each of the tables you selected and for all of the tables that are referenced from those tables.
  • Captures the database schema if you are creating entity beans from a database
  • Generates the sun-cmp-mappings.xml file
  • Maps the new beans to the database
  • Generates the appropriate getter and setter methods
  • Creates a JDBC connection pool and data source which is registered with the application server when you deploy the project

Mapping an existing entity bean to a database

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.

  1. If you do not have a database schema, capture the schema by selecting Databases > Schema in the New File wizard and saving the schema in the src/conf directory of your project.
  2. Double-click sun-ejb-jar.xml to open the Sun Configuration editor, expand the Sun Configuration node and select the bean you want to map.
  3. Click Advanced Settings in the CMP Mapping tab of the bean, choose the database schema from the combo box and click OK.
  4. Select the primary table for the bean from the Primary Table combo box in the CMP Mapping tab.
  5. Map your bean fields in the Field Mappings table by selecting a column or <multiple columns> in the Mapped Column combo box.

The IDE generates the sun-cmp-mappings.xml file when you save your changes.

Generating Database Tables in the IDE

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.

  1. Create a new entity bean. Right-click the EJB module's node and choose New > Entity Bean and make sure Container is specified as the persistence type. When you create a new CMP entity bean, the IDE automatically generates the local methods and the CMP field key.
  2. Add CMP fields. You can add CMP fields to your bean by right-clicking the CMP Fields node in the Projects window and choosing Add CMP Field, or by right-clicking in the class in the Source Editor and choosing EJB Methods > Add CMP Field from the contextual menu.
  3. Create relationships for your bean. Double-click the ejb-jar.xml to open the file in the visual editor, click the CMP Relationships tab and click Add to open the Add CMP Relationship dialog box.
  4. Configure deployment descriptors. Open the Sun Configuration editor, specify the JNDI name for your database resource and set Create Table at Deploy to True. To prevent an error when redeploying the application, set Drop Table At Undeployment to True. Alternatively, you can delete the tables manually in the Runtime window of the IDE.
  5. Deploy your application. When you create tables on deployment, these tables remain on the server until they are deleted.

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.

Editing CMP Mappings in the IDE

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:

Specifying primary and secondary tables

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.

Primary table

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.

Secondary tables

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.

  1. Select the secondary table from the combo box. The combo box lists the secondary tables available in the schema. When you select a secondary table, the IDE checks for a foreign key between the primary and secondary tables. If a foreign key between the tables exists, it is displayed as the reference key by default in the table. If the database schema has not defined a foreign key, you need to specify a reference key between the two tables.
  2. In the reference key table, select the column from the primary table you wish to use as the reference key.
  3. Select a column from the secondary table which you wish to link to the reference key and click OK.

Modifying field and relationship mappings

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:

  • Field. This column lists the fields to be persisted. These fields are selected in the CMP Fields and CMP Relationships of the enterprise bean. This column is always disabled. To add or remove the fields to be persistent fields, use the bean nodes in the tree view of the Sun Configuration editor.
  • Mapped Column. The Mapped Column enables you to change the mappings for the persistent field by modifying column mappings and field relationships. You can modify the mapping in the following ways:
    • Map to different field. You can change the current mapping of the field by selecting a different field from the available fields in the combo box.
    • Map fields to multiple columns. You can choose <map multiple columns> in the combo box to open the Mapping Fields to Multiple Columns dialog box.
    • Map relationship fields. If the mapping is a relationship, you can modify the relationship by selecting <map relationship field> in the combo box to open the Map Relationship Fields wizard.
  • Type. The Type column displays the data type of the selected Mapped Column. This column is disabled.
  • Fetch. This column specifies the fetch group configuration for fields and mappings. A field may participate in a hierarchical or an independent fetch group.

Additionally, the following mapping options are available in the CMP Mapping tab:

  • Automap All. You can map the fields on the currently selected enterprise bean by selecting Automap All. Automap All will make the most logical selections and update all the mappings including relationship fields. However, clicking Automap All will override all current mappings.
  • Unmap All. The Unmap All button will remove all mappings for the currently selected enterprise bean.

Mapping Fields to Multiple Columns

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:

  1. Add columns you want to map.
    In the left pane, select the columns you want mapped to the field and click Add. The selected columns appear in the right pane. The left pane lists the available columns from the tables mapped to the bean.
    • If you do not see the column you want to map, you might need to add a secondary table to your mapping, or change the primary table you have selected.
    • If no columns are listed, you have not yet mapped a primary table, or you have mapped a table that has no columns.
  2. Set column order.
    Use Move Up/Move Down to change the order of the columns in the right pane.
    If you map a field to more than one column, all columns will be updated with the value of the first column listed. Therefore, if the value of one of the columns is changed outside of an EJB application, the value will only be read if the change was made to that first column. Writing a value to the database overwrites any conflicting changes made to any other columns.

    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:

    • Field A mapped to Columns A and B, Field B mapped to Column B. Since the mappings only partially overlap, this example will get a validation error at compilation.
    • Field A mapped to Column A, and Field B mapped to Column B. Since there is no overlap, this mapping is allowed.
    • Field A mapped to Columns A and B, Field B mapped to Columns A and B. Since the mappings completely overlap, this mapping is allowed.
  3. Click OK.

Mapping Relationship Fields

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:

  1. In the first panel, you perform the intial setup for preparing to map the related beans, fields and primary table.
    • Verify that the Related Bean and Related Field are set. If not, then you need to cancel out of the editor and set them in the standard relationship setup for the bean before proceeding.
    • Verify that the Primary Table is set for the related bean.
    • Select between linking the tables directly, or through a join table.
  2. If you are linking the mapped tables directly in a 1:1 or 1:n relationship, you use the following wizard panel.
    • Map to Key
      This panel shows the following mapping information:

      • An existing mapping if there is one and there were no changes on the initial setup page.
      • A default mapping if there is no existing mapping or the mapping is no longer valid.

      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.

      • To create a compound key, use the Add Pair button.
      • If you want to map a relationship using a join table, go back to the previous panel and select Link the Mapped Tables with a Join Table.

    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.

  3. Click OK to exit the Map Relationship Field wizard.

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.


↑返回目录
前一篇: Case Study of NetBeans Profiler
后一篇: Color Sampler, Magnifier and Desktop Sampler Toolbar