Step 6. Generate O/R Mappings

Workshop provides a powerful and flexible object relational mapping interface to popular persistence services like Hibernate. Depending on the development scenario, O/R Mappings can be generated through different mechanisms:

In this step, we will follow both scenarios to generate O/R Mappings. The tasks in this step are:

To Create Object Model and Mapping Files from the Database Schema

In this step, we will use Workshop to automatically generate the associated object model and mappings by reverse engineering from the database schema.

  1. In the DbXplorer view, right-click within the white space of the view and select Generate Hibernate Mapping...

  2. In the Web Application dialog, select workshop-hibernate-tutorial and click OK.

  3. Select the database tables from the Hibernate ORM Generation dialog as shown below and click Next.

    Do not select the CONTACT database table. Later on in this tutorial you will use the CONTACT database table to generate Hibernate mapping files from the Contact object model to better understand the top-down development approach.

  4. The Table Associations dialog displays entity relationships as observed in the database schema from the foreign key definitions. The dialog allows you to edit a table association by selecting each association and modifying its options in the editing panel.

    Select the association between LINEITEM and ORDER_DATA.
    Change the value of the first Property field from orderData to order.

  5. Select the association between LINEITEM and PRODUCT.
    Uncheck the checkbox Generate a reference to a collection of Lineitem in Product. (This is in order to define a many-to-one unidirectional relationship between LINEITEM and PRODUCT.)

  6. Select the association between ORDER_DATA and CUSTOMER.
    Change the value of the second Property field from orderData to orders.
    Do NOT click Next yet.

To Create a new One-to-One Association between CUSTOMER and CUSTOMERID

Workshop also supports the creation of new associations. It can create Simple Associations (one to one, one to many, many to one) between two tables and Many to Many Associations through an intermediate table.


  1. Click the Create New Association option to specify the properties of the new association.

  2. In the Create New Association dialog, using the Table 1 drop down, select CUSTOMER.
    Using the Table 2 drop down, select CUSTOMERID.
    Click Next.

  3. In the Join Columns dialog, specify the join columns between tables.
    For the CUSTOMER table dropdown, select the CUSTOMERID column.
    For the CUSTOMERID table dropdown, select the CUSTOMERID column.
    Click Next.

  4. Select One-to-One to specify one customer per customerid.
    Click Finish.

  5. Since the new associations (shown in brown) are not observed in the database schema, they will be created programmatically in the Hibernate mapping file and object model.

    Click Next in the Hibernate ORM Generation dialog.

  6. In the Default Table Generation dialog, for the Key Generator drop down, select increment.
  7. Click Browse... next to the Java Package field.
    In the Choose Java Package dialog, click New Java Package.
    In the New Java Package dialog, in the Name field, enter com.bea.beans and click Finish.

    alt
  8. In the Choose Java Package dialog, click OK.
  9. In the Hibernate ORM Generation dialog, click Next.

  10. The Tables and Columns dialog defines the table and column mapping names for the generated mapping files and bean classes. By default, the schema's table and column names are used for the Hibernate mapping file and Java Beans. Note that when you hover over a table or column a tooltip displays the table and column properties as defined in the database schema.

    Click Next.

  11. In the Mapping Generation dialog you can choose the mapping file extension. You can also specify whether to generate the domain Java classes and whether to update the Hibernate configuration file hibernate.cfg.xml.

    Accept the defaults and click Finish.


  12. BEA Workshop will generate all of the Hibernate mapping files and Java Beans based on the properties supplied to the ORM Generation wizard.

    Once generation is complete, the Entities can be accessed through the AppXplorer view, inside the web (Web App Root) directory under the Hibernate Configuration branch of the project.

    Workshop remembers all the settings specified in Hibernate ORM Generation wizard to assist the next time you use it for the same database connection.

To Generate Spring Beans

At this point, you may optionally generate Spring beans and Spring configuration.

The parts of this task are:



To Review the Generated Mappings and Classes

  1. In the AppXplorer view, open the node workshop-hibernate-tutorial > web > Hibernate Configuration > Entities.
  2. You can observe the set of Persistence entities generated by Workshop. Workshop identifies the persistent entities based on the mapping resources specified in hibernate.cfg.xml file with extension .hbm.xml
  3. You can find the object model under your package in the branch web/WEB-INF/src/java


To Create Hibernate Mapping files from a Persistent Class

In this step, you will create a persistent java class Contact and use Workshop to generate Hibernate mappings for the Contact object. This way, we will follow a top-down development scenario.

  1. In the AppXplorer view, locate and open the node workshop-hibernate-tutorial > web/WEB-INF/src/java > com.bea.beans.
  2. Right-click com.bea.beans and select New > Class.
  3. In the New Java Class dialog, create a Java class Contact which implements the java.io.Serializable interface. (Click Add to add the interface java.io.Serializable. )

  4. Add the following set of private variables of type String to the Contact class. Note: We are defining properties that map to columns in the CONTACT database table.
    private String contactId;
    private String address;
    private String city;
    private String phone;
  5. Add no-argument constructor.
    public Contact( ) {
      super( );
    }
  6. Add getter and setter methods for each property by right-clicking inside the source pane and selecting Source > Generate Setters and Getters. In the Generate Getters and Setters dialog, click Select All and click OK.
  7. Add the following property.

    private static final long serialVersionUID = 1L;
  8. Add the equals( ) and hashCode( ) methods as we are going to use Contact instances in Set.
    public boolean equals(Object other) {
      if ( (this == other ) ) return true;
      if ( !(other instanceof Contact) ) return false;
      Contact castOther = (Contact) other;
      if( this.getContactId( ).equals(castOther.getContactId( )) ) {
        return true;
      } else {
        return false;
      }
    }

    public int hashCode() {
       return this.getContactId( ).hashCode( );
    }
  9. Save the Contact class. Now, we have the object representation for CONTACT persistence data. So next, we will create Hibernate O/R mapping files from the Contact object.
  10. From AppXplorer view, right-click the project workshop-hibernate-tutorial and select New > Other . Expand Hibernate and select Hibernate Mapping from Java Class and click Next.

  11. Click the Browse button for Java Class.
    In the Choose Type dialog, type Contact, select the com.bea.beans.Contact object, and click OK.

  12. In the Generate Hibernate Mapping dialog, click Next.

  13. In the Class Mapping Properties dialog, click Next.


  14. The Bean Properties Mapping dialog allows you to specify which bean properties should be mapped. If required, you can select the property and click Edit to specify the column mapping information or click the Remove button to unmap the property from the Hibernate mapping file. For this tutorial, we do not need to change any mapping information.
  15. Click Finish.

  16. Workshop generates the Hibernate O/R mapping file based on the information from the Contact object and maps to the CONTACT database table.

Click one of the following arrows to navigate through the tutorial:


Still need help? Post a question on the Workshop newsgroup.