8. Managing Persistent Entities with Entities Editor

Workshop allows you to view, create, and manage Hibernate entity relationships. The Entities Editor provides a centralized view of all entity relationships, allows for modification of entity properties, and simple navigation between the object model, Hibernate mapping, and database schema layers.

The tasks in this step are:

To Use the Entities Editor to View an Entity Relationship Diagram

As described previously, the Entities Editor displays an entity relationship diagram for entities defined in the project.

  1. In the AppXplorer view, expand the nodes workshop-hibernate-tutorial > web (Web App Root) > Hibernate Configuration. Workshop provides a list of entities under the Entities branch based on the entities defined in the Hibernate configurations file.
  2. Double-click on Hibernate Configuration > Entities Editor to access all of the entities and their properties.

  3. The Hibernate Entities Editor displays all entity relationships defined in the web application. Each box displays a specific entity, its properties, and an icon denoting the nature of the relationship.
  4. The Hibernate Entities Editor provides an opportunity to perform various UI operations like Zoom In, Zoom Out, and Auto Arrangement of diagram. Right-click on the Entities Editor and select any action to perform from the pop-up menu.
  5. View properties in the form of a tooltip by hovering your cursor over an entity.
  6. The Hibernate Entities Editor also provides facility to filter relationships between entities. This feature can help you to understand and review the entity relationship diagram when it is complex.

  7. Click the Filter button and unselect the options Many to One and One to Many to filter these relations between entities.

  8. Reset the filters.
  9. You can also review entities using the AppXRay view. Right-click the Product entity under Hibernate Configuration > Entities and select Show AppXRay Dependencies.


  10. It opens AppXaminer View showing the dependency diagram.


To Use the Smart Editor to Edit the Properties of Hibernate Persistent Entities

Using the Smart Editor in Properties view, Workshop provides an editing facility for standard properties of some objects.

  1. On the AppXplorer view, double-click on workshop-hibernate-tutorial > web (Web App Root) > Hibernate Configuration > Entities Editor to access all of the entities and their properties.
  2. Select the Product entity in the Hibernate Entities Editor view.
  3. Click the Properties view to see the Smart Editor. The Smart Editor displays the properties as defined in the Hibernate mapping file for the Product entity (Product.hbm.xml).

  4. In the Hibernate Entities Editor view, select the name property of the Product entity. The Smart Editor displays properties of the name property. View the properties.

  5. In the Smart Editor dialog, perform following changes to modify the Length property of property name.
  6. Click within the Hibernate Entities Editor view and press CTRL+SHIFT+S to save the changes.
  7. On the AppXplorer view, open the nodes workshop-hibernate-tutorial > web/WEB-INF/src/java > com.bea.beans.
    Right-click the file Product.hbm.xml and select Open With > Workshop XML Editor.

    Verify that the changes in the name property of the Product entity are reflected in hibernate mapping file Product.hbm.xml.

To Add New Properties to the Customer and Contact Entities

In addition to providing a view of the entity relationships for a project and a simple mechanism to update configuration of existing entities, the Hibernate Entities Editor also provides wizards to assist in defining new properties for a specific entity.

In this step, we will define a new One-to-Many Bi-directional Association between Customer and Contact entities.

We will add a New Property of type Customer in the Contact entity and a New Set Property in the Customer entity to hold a collection of Contacts. This way we can define a One-to-Many Bi-directional Association between the Customer and Contact entities.

  1. In the Hibernate Entities Editor view, right-click the Contact entity and select New Many-to-one to add a new property for the Contact entity to define a many-to-one relationship between Contact and Customer.

  2. In the Many to One Tag dialog, in the Property Name field, enter customer.
    In the Java Class field, enter com.bea.beans.Customer.
    In the Column Name field, enter CUSTOMERID.
    Click OK.

  3. Workshop will add the new code to the mapping file Contact.hbm.xml and update the Entities Editor to reflect the new content. You can observe a defined Uni-directional Many-to-One Association between Contact and Customer entities.

  4. Press CTRL+S to save changes.
  5. Right-click the Customer entity and select New Set... to add a new java.util.Set property for Customer entity. The Set property will maintain a list of contacts for the customer.

  6. In the Collection Mapping wizard, in the Property Name field, enter contacts.
    Click Next.

  7. In the Collection Mapping dialog, in the Entity Class field, enter com.bea.beans.Contact.
    In the Column Name field, enter CUSTOMERID.
    Place a checkmark next to the Inverse field.
    Click Finish.

  8. Workshop will add the new code to the mapping file Customer.hbm.xml and update the Entities Editor to reflect the new content. You can view a defined Bi-directional Many-to-One Association between Contact and Customer entities.

  9. Press CTRL+S to save the changes.
  10. Workshop displays 2 warnings in Problems view. The reason is that we have added properties and defined mappings in Customer.hbm.xml and Contact.hbm.xml files, but we have not defined corresponding properties in source code.

  11. In the Hibernate Entities Editor view, right-click the Contact entity and select Open Java Source.
    It opens the com.bea.beans.Contact.java file. You can also double-click to open the Contact.java file.

  12. In the Contact class add a property customer of type Customer and the corresponding getter/setter methods.
    private Customer customer;

    public Customer getCustomer( ) {
      return customer;
    }

    public void setCustomer(Customer customer) {
      this.customer = customer;
    }
  13. Save the Contact class.
  14. In the Hibernate Entities Editor, double-click the Customer entity to open the com.bea.beans.Customer.java file.
  15. Add the following import statement to the Customer class.
    import java.util.Set;
  16. Add the following code to the Customer class.
    private Set contacts;

    public void setContacts(Set contacts) {
      this.contacts = contacts;
    }

    public Set getContacts( ) {
      return contacts;
    }
  17. Save the Customer class.
  18. Verify that the warnings are cleared.

To Use the Hibernate Mapping Editor to Navigate to Specific Mapping Definitions (Optional)

We have observed the capabilities of Hibernate Entities Editor for displaying and managing the entities. In this step, we will learn how to navigate to the specific mapping definitions. We will also review the Hibernate Mapping Editor and understand validation and code completion facility provided by the editor.

  1. In the Hibernate Entities Editor view, right-click the OrderData entity and select Open Mapping Source to navigate to the Hibernate mapping file OrderData.hbm.xml.

  2. The OrderData.hbm.xml file appears in the Hibernate Mapping Editor - a combination of form and source editor which organizes mapping properties, provides forms to manage current properties, and includes wizards to assist in the creation of new content.

  3. When you select any property in the Hibernate Mapping Editor, the corresponding code will be highlighted in the source editor.

  4. In the source pane, change the customer mapping from

    <many-to-one
         name="customer"
         class="com.bea.beans.Customer"
         lazy="false"
    >

    to

    <many-to-one
         name="customer_NEW"
         class="com.bea.beans.Customer"
         lazy="false"
    >

    A WARNING icon appears with a message stating the field customer_NEW is not defined in the OrderData entity.

    Reset the value of name attribute of many-to-one element to customer. Save the mapping file.

  5. Move the cursor to the name attribute of the column element (inside the many-to-one element) and press CTRL+SPACE. The code completion feature of AppXRay displays the available columns as defined in database table.

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


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