Creating EJBs

You create and develop EJBs in the context of a Together project. There are two ways you can create EJBs in a project:

This section explains how to use each of these techniques. See also Overview of EJB Features.

Creating a project using existing EJB code

If you have existing EJB source code, you can create one or more Together projects around it. Together creates visual diagrams when it reverse engineers the code, and then keeps the visual model synchronized with subsequent changes to the code, and vice versa. Once the code is part of a Together project, you can quickly and easily generate up-to-date documentation.

Creating "one-click" EJBs

When your Together license enables EJB support, two EJB icons display in the Class diagram toolbar:

Entity EJB 

Entity bean: Creates elements in the visual model, and generates the underlying source code, for a default implementation of a persistent entity EJB with skeleton declarations for:

  • EJB implementation class, with:

    • Entity context attribute
    • One default field (integer type)
    • Default set of method declarations:
      • set & unset context methods
      • Activate & Passivate methods
      • Remove method
      • Store & Load methods
      • Create & PostCreate methods
      • findByPrimaryKey method
  • EJB Home interface, with:
    • Create method signature
    • findByPrimaryKey method signature
  •  

    EJB Remote interface

  • EJB Primary Key class, with:

    • default field (integer type)
    • primary key, hash code, and equals methods
  •  

    Dependency links

 

Session bean: Creates elements in the visual model, and generates the underlying source code, for a default implementation of a nonpersistent session EJB with skeleton declarations for:

  • EJB implementation class, with:

    • Session context attribute

    • setSessionContext method

    • Activate & Passivate methods

    • Remove method

    • ejbCreate method

  • EJB Home Interface

  • EJB Remote interface

No properties or Business Methods are declared. There are several ways to add them:

Home and Remote interfaces are hidden in the diagram by default, but you can see them in the Explorer (see Configuring for EJBs for more information). The interface names are automatically synced with the name of the implementation class.

To create a "one-click" EJB: 

  1. Create or open a Together project and create or navigate to the desired package.

  2. Create or open a Class diagram in the desired package.

  3. On the Class diagram toolbar, click the Entity EJB icon Entity EJB (to create that type) or the Session EJB icon (to create that type)

  4. Click the diagram background to generate the EJB elements as described above.

Customizing the default code for EJBs

The default code generated by one-click EJBs should be an adequate starting point for many developers. However, the default code is customizable by modifying the appropriate Code Template. You can customize the templates for Entity Bean class, Session Bean class, PrimaryKey class, Home Interface, and Remote Interface. For information on modifying these templates, see Using Code Templates.

Configuring EJBs using EJB Inspectors

Once you have a skeleton created by the on-click feature, you can use the EJB Inspector to edit it's properties.

To use the EJB Inspector:

  1. Select the EJB in the diagram or the Explorer.

  2. Press Alt - Enter or choose Properties from the speedmenu to display the EJB Inspector.

Using the EJB inspectors

You can develop EJB's visually using the EBJ Inspector for bean classes (speedmenu | Properties). The Inspector has pages common to all classes, but adds some specifically for working with EJBs.

The type-specific pages display a lower tabset that provide the means of specifying general properties, adding and removing business methods, defining references, and specifying bean type specific properties. Inspector for Entity EJB also has pages for defining Create and Finder methods.

 

 

Entity and Session EJB Inspectors

 

Sharing Home/Remote interfaces

You can optionally share the same set of home and remote interfaces between two or more EJB implementation classes of the same type (Session or Entity). You may want such capability in cases where you need to deploy differing implementations of the same interfaces on different servers.

Creating a second implementation class

In a case of shared interfaces, you should develop one complete implementation with home and remote interfaces. You can then create a second implementation class, refactor it into an EJB implementation, and specify the home and remote interfaces from the first bean in the properties of the new one.

To create a second implementation class:

  1. Create or open a Class diagram to show the new class.

  2. Create a new class in the diagram and name it as desired.

To refactor the class to an EJB implementation:

  1. Right-click and choose Choose Pattern from the speedmenu to display the Choose Pattern dialog.

  2. Locate the EJB Implementation folder and expand it.

  3. Select the Session EJB implementation or Entity EJB Implementation pattern and click Finish.

You can now begin developing the implementation, or you can proceed to specify the home and remote interfaces for the new bean class using the home/remote interfaces from the first implementation.

Note: In version 4.2 the EJB implementation patterns are missing from the Choose Pattern dialog. You must invoke the pattern from the Inspector field named Apply EJB Pattern.

To reuse the first implementations home/remote interfaces:

  1. In the properties Inspector for the new class, select the xxxEJB page (where xxx is either Session or Entity depending on the type of EJB).

  2. Choose the Do not synchronize names option button.

  3. In the Remote Name field, click the browse button to display the Select Element dialog.

  4. Use the Model node of the treeview to locate and select the remote interface belonging to the first implementation... that is, the remote interface that you want the bean you are working on to share.

  5. Click OK to accept the selection. Respond Yes to the Change Class? prompt.

  6. Do the same thing in the Home Name field, selecting the home interface for the first implementation in the Select Element dialog.

Showing second implementation class in separate Class diagram

If the second implementation class in the same Class diagram as the first, Implementation links are automatically drawn from the interfaces to the second implementation class. If the two implementations are in different Class diagrams, you mmay want to show the second implementation class in the Class diagram for the first implementation.

To show a linked second implementation class:

  1. Select the remote interface for the first implementation class.

  2. Choose Add Linked from the interface speedmenu. Linked elements are shown in the Add Linked tab of the Message pane.

  3. Select the second implementation class in the Message pane, right-click and choose Add.

A Class icon for the second implementation class is added to the diagram. You can now work on the second implementation class from either Class diagram.

Deleting implementation classes with shared interfaces

Normally, if you delete an EJB implementation class, home and remote interfaces are deleted along with the implementation class, both in source ond in the diagram. In cases where interfaces have been shared with another implementation class, deleting elements from the diagram does not automatically result in deletion of the relevant source code files... only the visual diagram elements are deleted.

You must explicitly delete implementation classes, and home/remote interfaces by selecting them in the Model tab of the Explorer and choosing Delete from the speedmenu.

Note: you can find an animated demoguide at www.togethercommunity.com.

See also:

Drawing diagram elements
Working with patterns