Creating Entity Beans with the Bottom-Up Approach


Starting with an existing relational database and generating entity beans that represent a natural mapping is fully automated in NetBeans IDE. From a single wizard, you can create CMP entity beans, generate CMP fields with types appropriate for the relational mapping, generate relationship fields (along with the relationship cardinality) based on the foreign key and index constraints, and provide the mapping information to the application server (see the top-down approach for details when performed manually). The wizard will detect joined tables, so there may not be a one-to-one mapping between tables and entity beans from the selected database. The wizard will also generate primary key classes if necessary.

To create a set of entity beans based on an existing database:

  1. In the Projects window, right-click the node of the EJB module that you want to add the beans to and choose New | CMP Entity Beans From Database.

  2. Complete the wizard to create the mappings.

The wizard contains two steps (see Figures 13-8 and Figure 13-9). The first step specifies how to obtain database metadata. The package for the generated Java classes is also required. The wizard also allows default generation of finder methods and exposing the fields in the component interface. Make sure the underlying database process is already started. If you are using the Derby database that is included with Sun Java System Application Server 8.2, you can start it from the IDE via the Tools | Derby Database menu item (Tools | Pointbase Database menu item if you are using version 8.1 of the application server).

Figure 13-8. Database Source and Beans Location page of the New File wizard for the CMP Entity Beans from Database template


Figure 13-9. Select Database Tables page of the New File wizard for the CMP Entity Beans from Database template


If the Generate Finder Methods for CMP Fields checkbox is selected, a singleargument finder method, along with the corresponding EJBQL query for selecting the collection of entity beans matching the input parameter, is generated for each CMP field.

Selecting the Add CMR and CMP Fields to the Local Interface checkbox will generate a method in the local interface for each CMP and relationship field. This typically is used in conjunction with the transfer object and transfer-object assembler pattern from a session facade to create domain objects based on entity beans. The transfer objects are standard serializable Java classes that represent the data and can be used outside of Java EE components. The transfer object assembler typically is a Java class that can create transfer objects (from an entity bean, in this case). A session facade typically is used to provide a coarse-grained set of services. A session facade can encapsulate entity bean access and reduce coupling (and increase performance) for clients.

The second step of the wizard selects the set of tables to include in the generation. The wizard will perform transitive closure (based on foreign key constraints) to ensure that relationships are generated. The most common scenario is to select a group of logically related beans to generate CMP beans for a single module.

After you select all the tables you need, the IDE will create a set of CMP beans, one per table selected (and maybe others that are part of the transitive closure of the foreign keys/primary keys of the selected table). The Sun Java System Application Server mapping will be correctly configured. You can still modify it to from the sun-ejb-jar.xml/sun-cmp-mapping.xml visual editor, as shown in Figure 13-10.

Figure 13-10. Visual editor for the sun-ejb-jar.xml/sun-cmp-mapping.xml files with a CMP bean selected




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net