A P P E N D I X A |
Working With Enterprise Beans |
The relationships between the elements of an enterprise bean can be intricate and complex. The Sun ONE Studio 5 IDE makes certain assumptions to preserve the integrity of your beans, but also flexibly supports various options for reusing beans. This appendix prescribes the best practices for working with existing enterprise beans.
To be certain that changes take place as you intended, make your edits through the enterprise bean's logical node and property sheets. If you use these recommended approaches, the IDE can ensure that the standards in the J2EE specification are followed.
These approaches are explained next.
As a general rule, it's best to go through the logical node of your enterprise bean to make changes in the bean's code. This node's icon looks like a coffee bean: The node was designed to organize all the elements of your enterprise bean.
When you work through the logical node, the IDE can most easily propagate the changes correctly throughout the bean.
All the classes in your bean are represented under this one node, as if the bean were a single object. There you can edit your enterprise beans without having to think about which class must receive each change. For example:
In many cases, the IDE can propagate your changes and synchronize your bean's classes and interface even when you go through other nodes of your bean to edit the Java code. However, you get the most consistent results by working through the logical node.
When you need to modify a method's name or return type, or when you need to edit or add a parameter or exception, the best place to do so is in the method's Customizer dialog box or property sheet. Under the logical node, select the method, right-click, and choose Customize or Properties.
Any change you make in one of these two places is validated and propagated in the right form to the right classes.
When you need to add code to complete a method, use the IDE's Source Editor.
You can create or modify any part of an enterprise bean by using the IDE's Source Editor exclusively. However, the IDE's wizards and other GUI tools are designed to save you work and to help prevent inconsistencies, so that you can produce standard, J2EE-compliant enterprise beans quickly.
In some cases, if you bypass the assistance offered by the EJB Builder, you might get mixed results. The EJB Builder tries to keep the changes you make in one class synchronized appropriately with the other classes, but the tool might not always be able to understand your intent and might not apply all the changes needed. Therefore, if you make direct changes to class code, the result might be an enterprise bean with errors that you must correct by hand.
![]() |
Caution - Once you start working on a class in the Source Editor, your enterprise bean is not saved until you explicitly save it. |
If you create code that is inconsistent with the J2EE specification, a warning badge or an error badge appears on a node's icon in the Explorer. To see a description of the problem, select the offending node, right-click, and choose Error Information or Validate EJB.
This yellow triangle warning badge on the logical node means that the bean or one of its classes might have a validation problem. Expand the logical node to see where the problem lies. For instance, a method defined in the remote interface might not be in the bean class, or a class might extend the wrong Java superclass. Even if you can compile the bean, it will encounter problems.
This red X error badge on the logical node means that the bean or one of its classes might have a severe problem. For instance, an entire class could be missing. A bean bearing this error badge will not execute or even allow interaction.
The EJB Builder contains a custom compiler that validates your enterprise beans against the Enterprise JavaBeans Specification. You can decide whether to have compilation and validation done separately. However, when you select an enterprise bean node, right-click it, and choose Validate EJB, the default action is to compile the bean and then immediately validate it.
Notice that compilation alone does not catch all errors, and that if a bean has compilation errors, it is not validated.
Validation and compilation in the IDE serve different purposes. When you only compile an enterprise bean, the IDE compiles the various classes that constitute the bean. If these individual classes have Java code that is syntactically correct, the bean can compile without errors, even if one class is not consistent with another or with the J2EE specifications. To ensure consistency between the elements of an enterprise bean, you must also validate the bean.
The EJB Compiler conforms to the IDE's definitions of build, compile, and clean.
In most cases, you can save considerable time by using the EJB Compiler with its default options during your iterative development cycle. However, under certain conditions (for example, if you must change a superclass after a successful compilation), compilation does not catch Java or EJB validation errors. In such cases, you might need to perform a build (possibly traversing several directories, depending on the location of all your bean's Java files) to detect or resolve compilation errors.
The compiler also does some semantic checking of the EJB QL in the select and finder methods of a CMP entity bean. Again, you can choose whether or not to have the compiler require EJB QL statements. Sometimes, you might find it convenient to turn off that requirement, so that you can develop, compile, and validate your enterprise bean without receiving error messages about missing EJB QL statements.
However, you must add the EJB QL (and turn the compiler's EJB QL requirement option back on) before you deploy the bean to the IDE's embedded application server, Sun ONE Application Server 7. This server and some others require EJB QL, which the server plugin converts to the SQL it needs in order to run queries in your finder and select methods.
To set compilation and validation options for your enterprise bean, do as follows:
1. From the main menu, choose Tools Options.
2. Expand the Options node, the Building node, and the Compiler Types node.
4. In the property sheet, make your selections.
The default setting for both fields is True. Change the setting in the following cases:
If you have chosen to separate validation from compilation, validate your bean as follows:
In the Explorer's Filesystems tab, select the bean's logical node, right-click, and choose Validate EJB.
Depending on the size of your bean, validation might take a few moments. When it's finished, an output window opens and displays messages about your bean.
When you compile an EJB module or do anything to a module that involves a compile (such as exporting an EJB JAR file, or calculating the class files for export), the module and all its component beans are validated automatically.
In many cases, your work is saved automatically. However, this is not always true. For example, compiling a bean does not always save it. When you exit from the IDE, notice the confirmation dialog box and save the work you want to. For best results, use File Save All periodically while you work in the IDE.
When you rename a bean, you don't have to manually rename all the bean's related objects and their internal references. Use the IDE's GUI support as explained next, and the IDE synchronizes all interfaces automatically (both their external object names and related references within the source code). Do as follows:
1. Select the bean's logical node, right-click, and choose Rename.
The Rename dialog box appears. As soon as you begin typing into the New Name field, the checkbox options are activated.
2. Use the checkboxes to rename all the bean's related objects at once.
However, if you obtained one of your bean's objects from elsewhere, carefully consider whether or not you want to rename it. For example, if two or more entity beans share the same home and remote interfaces, you might want to keep similar interfaces named the same wherever they occur.
![]() |
Caution - If you rename any one of the related objects independently, connections between the objects can be lost. |
An enterprise bean's class can be based on a class from another bean. For example, you can create an enterprise bean that uses another bean's remote interface. When you modify a class that is based on another class, you are actually modifying the original class. The more recent bean points to the earlier bean's class. You are not working with a separate copy of the class file. This is part of the IDE's design to promote easy reuse of enterprise bean elements.
When you copy and paste a bean into another package, the IDE creates a node in the new package that points to classes and interfaces in the original package. The IDE does not make the assumption that you want everything in one package; some organizations need to reuse bean elements more flexibly. Therefore, if you want an exact copy of one bean in another package, you must also ensure that the paths of all the bean's classes and interfaces are changed to the new package.
To copy and paste an enterprise bean from one package to another, do as follows:
1. In the Explorer's Filesystems tab, right-click the logical node of the bean you want to copy and choose Copy.
2. Right-click the package to which you want to copy the bean and choose Paste Copy.
3. Expand the Classes node under the original bean's logical node, right-click the node for a class or interface, and choose Copy. Then paste the class or interface into the destination package by right-clicking the destination package and choosing Paste Copy.
Repeat this step for each of the bean's classes and interfaces.
4. Right-click the logical node of the copied bean and choose Properties.
5. In the property sheet, update each class to point to the destination package. Find the following fields:
a. Click each field, then click the ellipsis (...) button.
b. In the property editor, navigate to the destination package, select the copy of the class or interface you pasted into the package, and click OK.
The property value changes to PackageName.classname.
When you're done, each class and interface property has the package name appended to the original name. The destination package contains a complete copy of the bean.
After you create an enterprise bean, you might need to change it to use an element from another bean, such as a home or remote interface. To do this, use the bean's property sheet as follows:
1. In the Explorer window, select the bean that needs a different class or interface, right-click, and choose Properties.
2. In the Properties tab, click the appropriate property (Bean Class, Home Interface, Primary Key Class, or Remote Interface), and click the ellipsis (...) button.
3. Navigate to the class or interface you want to use, select it, and click OK.
The property field shows the fully qualified path name of the new class or interface. Your bean does not contain a copy of the new element. It merely points to the original element.
When you have added a method using the GUI support available from the Explorer window, you can edit the method in the Source Editor. If all you need to do is complete the body of the method in the bean class, and your edits don't affect any other class or interface, you should use the Source Editor. However, for changes that might have a ripple effect on other classes, you might need to synchronize the changes in the bean's related objects. For examples, see Using the Source Editor to Edit Beans.
Or, you can simply use the Customizer dialog box as follows:
1. In the Explorer window, expand the logical node of the bean whose method you want to edit. Navigate to the method.
2. Select the method, right-click, and choose Customize.
The Customizer dialog box appears with the same fields as the New Method dialog box.
3. Edit the fields as needed. Click Close when you're done.
The IDE propagates your changes throughout the bean.
To see any method you have created for an enterprise bean, expand the bean's logical node and navigate to the sub-node for the kind of method you want to view. Right-click the method's node and choose Open. The Source Editor opens the class directly to the method code.
Depending on whether your entity bean uses container-managed or bean-managed persistence, you use different methods to rename a field and to change a field's type.
In a CMP bean, use the GUI support available from the Explorer window. Do as follows:
1. Expand the bean's logical node and select the CMP field. Right-click, and choose Rename.
2. Use the checkboxes to specify the extent of your change.
In a BMP bean, use the Source Editor to rename a persistent or a nonpersistent field.
In a CMP bean, use the Explorer's GUI support. Change a field's type as follows:
1. Expand the bean's logical node and select the CMP field. Right-click, and choose Customize.
2. In the Customizer dialog box, select another type.
In a BMP bean, use the Source Editor to change the type of a persistent or a nonpersistent field.
Regardless of the type of enterprise bean, delete a bean only as follows:
1. Select the bean's logical node, right-click, and choose Delete.
The Confirm EJB Deletion dialog box appears.
2. Use the checkboxes to confirm that you want to delete all the bean's related objects at once.
If one of your bean's related objects is used elsewhere, carefully consider whether or not you want to delete it. For example, if several entity beans use the same primary-key class, you should deselect its checkbox before deleting the rest of the bean's classes.
![]() |
Caution - Don't choose Edit |
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.