Previous Next vertical dots separating previous/next from contents/index/pdf

6. Generate O/R mappings

Workshop Studio 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 the scenarios to generate O/R Mappings.

6.1. Create Hibernate O/R mapping files (.hbm.xml) and object model using Hibernate ORM Generation wizard

6.1.1. Create object model and mapping files from the database schema

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

  1. In DbXplorer, right-click and select Generate Hibernate Mapping...

  2. Select the Hibernate Web application project, Workshop-Hibernate-Tutorial, from the Web Application dialog. Workshop considers a web application to be a Hibernate application if it has the Hibernate configuration file hibernate.cfg.xml at the root level of an Eclipse source folder. Similarly, Workshop identifies a JPA web application based on the existence of a persistence.xml file.

  3. Click OK.
  4. Select the database tables from the Hibernate ORM Generation dialog as shown below. Note that we are not selecting the CONTACT database table at present as we will generate Hibernate Mapping files from the Contact object model and that way we will understand the Top-Down development approach.

  5. Click Next
  6. The Table Associations dialog displays entity relationships as observed in the database schema from the foreign key definitions (shown in green). The dialog allows you to edit a table association by selecting each association and modifying its options in the editing panel.

  7. Select the association between LINEITEM and ORDER_DATA. Edit the property from orderData to order that references OrderData in LineItem.

  8. Select the association between LINEITEM and PRODUCT. We want to define many-to-one unidirectional relation between LINEITEM and PRODUCT. Hence, uncheck the checkbox for generating reference to a set of LineItems in Product.

  9. Select the association between ORDER_DATA and CUSTOMER. Edit the property from orderDatas to orders that references OrderData in Customer.

6.1.1.1. Create a new One-to-One Association between CUSTOMER and CUSTOMERID

  1. 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. Click the Create New Association option to begin specifying the properties of the new association.

  2. We want to define a simple one-to-one association between the CUSTOMER and CUSTOMERID tables. In the Create New Association dialog, select the CUSTOMER and CUSTOMERID tables as shown below and click Next.

  3. Select the CUSTOMERID column for CUSTOMER table and CUSTOMERID column for CUSTOMERID table to specify join columns between tables and click Next.

  4. Select One-to-One radio option to specify one customer per customerid.

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

  6. Click Next in Hibernate ORM Generation dialog.
  7. In the Default Table Generation dialog, select increment strategy for the key generation.

  8. A Java Package is required for generating the object model classes. Click the Browse... button beside Java Package and create a new Java package com.bea.beans using the New Java Package link.

  9. 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. Notice that tooltips display the table and column properties as defined in the database schema.



    Customize the tables and columns mapping generation as described below:

  11. Click Next
  12. 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 up update the Hibernate configuration file hibernate.cfg.xml.

  13. Click Finish
  14. BEA Workshop will generate all of the Hibernate mapping files and Java Beans based on the properties supplied to the ORM Generation wizard.
  15. Once generation is complete, the Entities can be accessed through the AppXplorer view, inside web (Web App Root) under the Hibernate Configuration branch of the project.
  16. Workshop remembers all the settings specified in Hibernate ORM Generation wizard to assist the next time you use it for the same database connection.

6.1.2 Generate Spring Beans

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

6.2. Review the generated mappings and classes

  1. In AppXplorer, go to the Hibernate Configuration branch under the Workshop-Hibernate-Tutorial project.
  2. You can observe the set of Persistence entities generated by the 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

6.3. 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 Top-Down development scenario.

  1. Under the package com.bea.beans, create a Java class Contact which implements the java.io.Serializable interface.

  2. Add following set of private variables of type String to Contact class. Note: we are defining properties that maps to columns in CONTACT database table.
    private String contactId;
    private String address;
    private String city;
    private String phone;
  3. Add no-argument constructor.
    public Contact( ) {
      super( );
    }
  4. Add getter and setter methods for each property.
  5. Override 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( );
    }
  6. 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.
  7. From AppXplorer view, click on the project Workshop-Hibernate-Tutorial. Select File > New > Other . Expand Hibernate and select Hibernate Mapping from Java Class and click Next.

  8. Click Browse... button for Java Class and select the com.bea.beans.Contact object to be mapped.

  9. Check the name for the automatically generated mapping file, and click OK. Click Next.

  10. In the Class Mapping Properties dialog, browse and select the CONTACT table as mapping database table and select the Java property contactId as primary key property. Click Next.

  11. 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. At present we do not need to change any mapping information.
  12. Click Finish.
  13. Workshop generates Hibernate O/R mapping file based on the information from the Contact object and maps to CONTACT database table.

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

 

Skip navigation bar   Back to Top