# How Do I: Add a Relation to an Entity Bean?

Entity beans can have relationships with other entity beans. For instance, a Customer bean can have various relationships with an Address bean, a CreditCard bean, and so forth. Relationships can only be defined between entity beans representing tables in the same datasource, and the entity beans' local (business) interfaces must have been declared. The current topic describes how to add a relation between CMP entity beans.

To Add a Relation

1. Make sure that the entity bean is displayed in Design View, and that WebLogic server is started.
2. Right-click the right-hand area of the bean, and choose Add Relation.
3. If the entity bean does not have a bean class name defined for the local interface, a Set Local Interface dialog first prompts you to supply one. For example, for a Customer bean the local interface would be declared by convention as CustomerLocal. Otherwise, the Add a Relation dialog appears.
4. Select the Target EJB from the drop-down list in the right-hand panel. If the target entity bean does not have a local interface, you will similarly be prompted to supply one.
5. Enter the Source CMR (Container-Managed Relation) Field Name and Type in the left-hand panel.
6. For bidirectional relations, enter the Target CMR Field Name and Type in the right-hand panel. For unidirectional relations, select the Source Bean Multiplicity from the drop-down list in the left-hand panel. For example, a relation between a customer and his/her various phones number might at this point be defined like shown in the picture below. Click Next.

7. Depending on the type of relation you are defining, either a join table or an additional column (or columns) in either entity bean's table is needed to store primary key information. WebLogic Workshop can create the join table or additional column for you if these are not defined yet. Alternatively, you can enter the name of an existing join table or column for the relation. In our example, a Customer_ID column already existed in the Phone table to map the relation, and the relation was accordingly mapped as shown below.

8. After mapping the relation, give the new relation a name, and click Finish.

The relation is now created in both entity beans. To examine the relation, right-click the relation name and choose edit in Source View. You will be directed to the corresponding @ejbgen:relation tag. If applicable, accessor methods are also declared for the relation. In our example, the following accessor methods have been created in the Customer bean:

        /**
         * @ejbgen:cmr-field
         * @ejbgen:local-method
         */
        public abstract java.util.Collection getPhoneNumbers();

       /**
        * @ejbgen:local-method
        */
       public abstract void setPhoneNumbers(java.util.Collection arg);

Related Topics

Entity Relationships

@ejbgen:relation Annotation

How Do I: Test an Enterprise JavaBean?

How Do I: Use an Enterprise JavaBean in a Web Application?

Developing Enterprise JavaBeans