Previous Next
sde-vs_orm_user_guide Chapter 18 - Data Model

Chapter 18 - Data Model

You are provided with a visual modeling environment for the data model of an application, and also reverse database engineering. This chapter shows you how to depict the object models by using Entity Relationship Diagram and how to reverse database, and describes how the data model maps to the object model.

In this chapter:

Introduction

An entity is an object in the business or system with well-defined characteristics which are represented by columns showing what information can be stored. In relational databases, an entity refers to a record structure, i.e. table.

A data model provides the lower-level detail of a relational database of an application. It shows the physical database models and their relationships in an application. An entity relationship diagram can be used to describe the entities inside a system and their relationships with each other; the entity relationship diagram is also known as a data model.

Visual modeling for data models is supported, not only by creating a new data model, but also by transforming from an object model. Reversing the existing relational model into data models is also supported. As object-relational mapping is automated, object model can thus be generated from the data model by reversing the existing database.

The following section describes how to reverse the relational model into data models and depict the data models using the Entity Relationship Diagram.

Entity Relationship Diagram

Entity relationship diagram is a graphical representation of a data model of an application. It acts as the basis for mapping the application to the relational database.

Creating an Entity Relationship Diagram

You are provided with three ways to create an Entity Relationship Diagram.

  1. Drawing an Entity Relationship Diagram(ERD)
  2. Reverse Engineering an existing Relational Database
  3. Synchronizing from the Object Model to Data Model

Drawing an Entity Relationship Diagram (ERD)

  1. You can create a new ERD in one of the three ways:
  2. A new Entity Relationship Diagram pane is displayed.

Creating a new Entity element to the ERD
  1. On the diagram toolbar, click the Entity shape icon.
  2. Figure 18.3 - Entity icon button
  3. Click a location in the diagram pane.
  4. An icon representing the entity element is added to the diagram.

  5. Type in a name for the Entity element.
Modifying the Entity Specification
  1. To display the Entity Specification, right-click the entity element, click Open Specification.
  2. Entity Specification dialog box is displayed, you can modify the entity properties and constraints.

    Figure 18.4 - Entity Specification dialog
Adding new Column to the Entity
  1. You can add a new column to the entity in one of the three ways:

A new column is added, type the column name and type in the form of "column_name: type". You can also edit the column name by double-clicking the column name or by pressing the F2 button.

Adding Relationship to the Entities

You can add relationship to the entities in one of the two ways:

A line indicating a connection between the two entities is shown.

Editing Relationship Specification

You can edit the relationship specification in the following way:

Reverse Engineering an existing Relational Database

You can create an Entity Relationship Diagram by reverse engineering an existing relational database.

The Database to Data Model dialog box is displayed.

Step 1: Select Language

Select the language of the project from the drop-down menu, either Java or C#, and then click Next > to proceed to Step 2.

Figure 18.10 - Database to Data Model dialog
Step 2: Database Configuration

You can configure the database connection for the desired database to be reversed.

  1. You are asked to define the database configuration. Refer to the descriptions in the Database Configuration chapter for information on how to configure the database.
  2. Figure 18.11 - Database Configuration
  3. Click Next>, go to Step 3 of Reverse Database.
Step 3: Selecting Tables

All the available tables found from the connected database are listed.

  1. Select the tables that you want to reverse to Data Model.
  2. Click Finish.
  3. Figure 18.12 - Selecting Tables

    An Entity Relationship Diagram is automatically generated and displayed. It can be found under the Diagram Navigator.

    Figure 18.13 - The ERD formed by the reverse entities

Synchronizing from an Object Model to a Data Model

You can generate the ERD from a class diagram by synchronization if there is a class diagram.

  1. You can synchronize the Class Diagram to ERD in one of the three methods:

Specifying Index Column

If a relationship with cardinality of many at one end, a corresponding collection class will be used for handling its multiple cardinality. You can specify an index column to sort the collection.

  1. Right-click on the connection line, click Open Specification from the pop-up menu.
  2. Figure 18.18 - Open the relationship specification

    Relationship Specification dialog box is displayed.

  3. Check the option for Ordered.
  4. Select the index column from the drop-down menu of Index column, click OK.
  5. Figure 18.19 - Relationship specification dialog
    You can select Create Column from the drop-down menu to create a new index column for sorting.

Using the ID Generator

As the primary key is unique, the generation of primary key is supported. The ID Generator is specialized for generating a primary key value at runtime.

  1. Right-click on the primary key of an entity, select Open Specification from the pop-up menu.
  2. Figure 18.20 - To open Column specification

    Column Specification of the primary key is displayed.

  3. Select the ID generator from the drop-down menu of ID Generator, click OK to confirm setting.
  4. Figure 18.21 - Column specification dialog
    If the ID Generator is specified as either sequence, seqhilo or hilo, you have to enter the key for the sequence/table name.

Defining Discriminator

In generalization, the superclass distributes its commonalities to a group of similar subclasses. The subclass inherits all superclass's attributes and it may contain specific attributes. The entities within the hierarchy are combined into one single entity containing all the attributes and a discriminator column. The discriminator contains a unique value which is used for identifying the entity which hierarchy it belongs to.

You are allowed to define the discriminator in the entity and discriminator value in the classes.

Defining Discriminator Column for Entity

You can add a new column acting as the discriminator column for an entity.

  1. Right-click on an entity, select New Column.
  2. Figure 18.22 - Create a column
  3. Enter the name and type for the discriminator in the form of "discriminator_name: type".
  4. Figure 18.23 - Enter the column name and type
  5. Right-click on the entity, select Open Specification....
  6. Figure 18.24 - Open the column specification

    Entity Specification dialog box is displayed.

  7. Select the desired column from the drop-down menu of Discriminator Column, click OK to confirm setting.
  8. Figure 18.25 - Column Specification dialog

Defining Discriminator Value for Class

You can specify the discriminator value for each sub-class.

  1. Right-click on the relative sub-class for adding discriminator, select ORM > ORM Class Detail...from the pop-up menu. The Class Specification dialog box showing the ORM Class Detail tab is displayed.
  2. Figure 18.26 - To open ORM Class Detail/td>
  3. Enter the discriminator value for identifying the sub-class.
  4. Figure 18.27 - Class Specification dialog

Creating an Array Table

In a one-to-many relationship, a collection is used for handling the multiple objects such that it is simpler to retrieve each object from the collection one by one.

The idea of Array Table is promoted which allows users to retrieve objects in the form of primitive array, instead of a collection when handling a data column with cardinality of many.

You are allowed to create an array table in the entity and define an array type in the classes.

Defining an Array Table

You can create an Array Table for the Entity with a column containing more than one instance of data.

  1. Create a one-to-many relationship between the entity and one of its columns that may contain more than one instance of data.
  2. Figure 18.28 - Entities with one-to-many relationship

    In the above case, the phonebook has a contact entry for each contact person. Each contact person may have more than one phone numbers. A one-to-many relationship between contact entry and contact phone can be built.

  3. Right-click on the entity for the data column with cardinality of many, select Convert to Array Table from the pop-up menu.
  4. Figure 18.29 - Convert to Array Table
  5. A warning message will be displayed, showing that the listed constraints are not satisfied for converting to array table. Click Yes to let SDE-VS to resolve the constraints automatically. Click No to cancel the conversion to array table.
  6. Figure 18.30 - Warning message for the constraints are not satisfied

    The conversion to Array Table is completed and the entity for the data column is stereotyped as Array Table.

    Figure 18.31 - Entity are converted to Array Table

Defining an Array Type for Attribute in Class

A class with an attribute of array type modifier means that the attribute may contain more than one data; thus it implies the idea of Array Table.

You can define the array type for the attribute in one of the two ways:

Creating a Partial Table

In a one-to-one identifying relationship, an entity may be a subordinate of the related entity; that is, the subordinate entity has columns which also belong to its superior entity in the real world situation.

The idea of Split Table with stereotype of Partial is also promoted, which allows developers to optimize the size of database, and minimizes the redundant persistent classes for handling one-to-one identifying relationship. In order to reduce the risk of appending a new column to an existing database table, Split table supports developers to add new columns to the partial table with a one-to-one identifying relationship linked to the existing table.

You are allowed to split the entity into two and convert the subordinate entity to be a Partial Table in a one-to-one identifying relationship.

Splitting Table

You can split an entity into two associated with a one-to-one identifying relationship.

You can activate the Split Table dialog box in one of the two ways:

Converting to a Partial Table

You can convert an entity to a Partial Table in a one-to-one identifying relationship.

  1. Right-click on the entity, select Convert to Partial Table from the pop-up menu.
  2. Figure 18.40 - Convert to a partial table

    The entity is stereotyped as Partial.

    Figure 18.41 - The entity is convert to a partial table

Copying SQL statements from Tables

Copying SQL statements from the ERD entities is provided. It allows the developers to copy the SQL statements from the entity relationship diagram easily such that developers can use and modify the SQL statement on the database directly.

In order to copy the SQL statement, you must configure the database setting in advance as the SQL statements will be generated according to the default database server type.

  1. To configure database connection, on the menu, click Modeling > ORM > Database Configuration....
  2. Figure 18.42 - To open database configuration

    Database Configuration dialog box will be displayed. Refer to the descriptions in the Database Configuration chapter for information on how to configure the database.

SDE-VS will only provide you the function of copying SQL if the default database connection is set.
If there are multiple database settings, the SQL statements will be generated for all these database servers.
Example:

There are two database settings selected in the working environment.

Figure 18.43 - Set 2 database setting
  1. Right-click on the ERD, select Copy SQL > Detail...from the pop-up menu.
  2. Figure 18.44 - To copy a SQL
    You can select Create Table(s), Drop Table(s), Select, Insert, Update and Delete from the Copy SQL submenu to directly copy the SQL statements to clipboard.
  3. Generate SQL dialog box is displayed, select the database server from the drop-down menu of Database, the corresponding SQL statements will be displayed accordingly.
  4. Figure 18.45 - Generate SQL dialog

    You are allowed to copy the SQL statements from the Generate SQL dialog box.

Copying SQL statements from Specified Scope

You can specify the scope on the ERD to generate the SQL statements.

You can specify one of the three scopes:

Mapping a Data Model to an Object Model

Object Relational Mapping (ORM) is supported which maps data models to object models and vice versa.

Mapping between objects to relational database preserves not only the data, but also the state, foreign/primary key mapping, difference in data type and business logic. Thus, you are not required to handle those tedious tasks during software development.

Mapping Entities to Classes

All entities map one-to-one to persistent classes in an object model.

Example:

Figure 18.50 - Mapping entities to classes

In the above example, the Customer Entity map one-to-one the Customer Class as the Customer instance can store the customer information from the Customer Entity.

Mapping Columns to Attributes

Since all entities map one-to-one to persistent classes in an object model, columns in turn map to attributes in a one-to-one mapping. All specialty columns such as computed columns and foreign key columns are ignored during the transformation.

Example:

Figure 18.51 - Mapping columns to attributes

In the above example, the following table shows the mapping between the columns of the Customer Entity and the attributes of the Customer Class.

Customer Entity Customer Class
CustomerID CustomerID
CustomerName CustomerName
Address Address
ContactPhone ContactPhone
Email Email
Table 18.1

Mapping Data Type

The column data type automatically maps to an appropriate attribute type of object model.

Example:

Figure 18.52 - Mapping data types

In the above example, the following table shows the mapping between data types

Customer Entity Customer Class
int (10) int
varchar(255) String
Table 18.2

A table shows the data type mapping between Object model and Data model.

Data Model Object Model
Bigint Long
Binary Byte[]
Bit Boolean
Blob Blob
Varchar String
Char Character
Char(1) Character
Clob String
Date Date
Decimal BigDecimal
Double Double
Float Float
Integer Integer
Numeric BigDecimal
Real Float
Time Time
Timestamp Timestamp
Tinyint Byte
Smallint Short
Varbinary Byte[]
Table 18.3

Mapping Primary Key

As the columns map to attributes in a one-to-one mapping, primary key columns in the entity map to attributes as a part of a class.

Example:

Figure 18.53 - Mapping primary key

In the example, the primary key of entity Product, ProductID maps to an attribute ProductID of the class Product.

Mapping Relationship

Relationship represents the correlation among entities. Each entity of a relationship has a role, called Phrase describing how the entity acts in it. The phrase is attached at the end of a relationship connection line. The phrase maps to role name of association in the object model.

There are two types of relationships in data model mapping to object model - identifying and non-identifying.

Identifying relationship specifies the part-of-whole relationship. It means that the child instance cannot exist without the parent instance. Once the parent instance is destroyed, the child instance becomes meaningless.

Non-identifying relationship implies weak dependency relationship between parent and child entities. There are two kinds of non-identifying relationships, including optional and mandatory. The necessity of the parent entity is "exactly one" and "zero or one" in the mandatory and optional non-identifying relationship respectively.

Mapping Identifying Relationship

Since the identifying relationship specifies the part-of-whole relationship, it map to composite aggregations which implies that the part cannot exist without its corresponding whole.

Example:

Figure 18.54 - Mapping identifying Relationship

In the above example, the identifying relationship between the entities of EmergencyContact and Student maps to composition aggregation.

Mapping Non-identifying Relationship

Since non-identifying relationship implies weak relationship between entities, it maps to association.

Example:

Figure 18.55 - Mapping non-identifying relationship

In the above example, non-identifying relationship between entities Owner and Property maps to association between Classes of Owner and Property.

Mapping Cardinality

Cardinality refers to the number of possible instances of an entity relate to one instance of another entity. The following table shows the syntax to express the Cardinality.

Table shows the syntax expressing the Cardinality

Type of Cardinality Description
Zero or one instance
Zero or more instances
Exactly one instance
One or more instances
Table 18.4

Table shows the typical mapping between Entity Relationship Diagram and Class Diagram.

Entity Relationship Diagram Class Diagram
Table 18.5

Mapping Many-to-Many Relationship

For each many-to-many relationship between entities, a Link Entity will be generated to form two one-to-many relationships in between. The primary keys of the two entities will automatically migrate to the link entity to form the primary/foreign keys.

Example:

Figure 18.56 - Mapping Many-to-many relationship

In the above example, SDE-VS generates the link entity once a many-to-many relationship is setup between two entities. To transform the many-to-many relationship, the many-to-many relationship maps to many-to-many association.

Mapping Array Table to Collection of Objects

The Array Table is promoted to allow users retrieve objects in the form of primitive array.

When transforming an array table, the array table will map to an attribute with array type modifier.

Example:

Figure 18.57 - Mapping Array Table to Collection of Object

In the above example, the phonebook has a contact entry for each contact person. Each contact person may have more than one phone numbers. The array table of ContactEntry_Phone maps into the phone attribute with array type modifier in the ContactEntry class.

Mapping Data Model Terminology

The following table shows the shift from data model to object model terminology.

Data Model Term Object Model Term
Entity Class
Instance of an entity Object
Relationship Association
Supertype/subtype Generalization
Column Attribute
Phrase Role
Cardinality Multiplicity
Table 18.6

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com