Step 7. 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.
- 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.
- Double-click on Hibernate Configuration > Entities
Editor to access all of the entities and their properties.
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.
- 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.
- View properties in the form of a tooltip by hovering your cursor over an
entity.
- 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.
Click the Filter button and unselect the options Many
to One and One to Many to filter these relations
between entities.
![](img/img80.jpg)
- Reset
the filters.
- You can also review entities using the AppXRay view. Right-click
the Product entity
under Hibernate Configuration > Entities and select Show
AppXRay Dependencies.
![](img/img80_1.jpg)
It opens AppXaminer View showing the dependency diagram.
![](img/img80_2.jpg)
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.
- 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.
- Select the Product entity in the Hibernate Entities
Editor view.
- 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).
![](img/img73.jpg)
- 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.
![](img/img75.jpg)
- In the Smart Editor dialog, perform following changes
to modify the Length property
of property name.
- Select the checkbox for Not Null option
- Update the Length value from 100 to 150
![](img/img76.jpg)
- Click within the Hibernate Entities Editor view and press CTRL+SHIFT+S to
save the changes.
- 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.
![](img/img77.jpg)
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.
- 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.
![](img/img81.jpg)
- 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.
![](img/img82.jpg)
- 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.
![](img/img83.jpg)
- Press CTRL+S to save changes.
- 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.
![](img/img84.jpg)
- In the Collection Mapping wizard, in the
Property Name field, enter contacts.
Click Next.
![](img/img85.jpg)
- 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.
![](img/img86.jpg)
- 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.
![](img/img87.jpg)
- Press CTRL+S to save the changes.
- 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.
![](img/img88.jpg)
- 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.
![](img/img89.jpg)
- 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;
} |
- Save the Contact class.
- In the Hibernate Entities Editor,
double-click the Customer entity to open the com.bea.beans.Customer.java file.
- Add the following import statement to the Customer class.
- Add the following code to the Customer class.
private Set contacts;
public void setContacts(Set contacts) {
this.contacts = contacts;
}
public Set getContacts( ) {
return contacts;
} |
- Save the Customer class.
- 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.
- 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.
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.
![](img/img91.jpg)
- When you select any property in the Hibernate Mapping
Editor, the
corresponding code will be highlighted in the source editor.
![](img/img92.jpg)
- 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.
![](img/img93.jpg)
- 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.
![](img/img94.jpg)
Click one of the following arrows to navigate through the tutorial:
Still need help? Post a question on the Workshop
newsgroup.