Previous Next
sde-vs_orm_user_guide Chapter 20 - Implementation

Chapter 20 - Implementation

Class Diagram, Entity Relationship Diagram and Database can be used to generate the persistent code. This chapter shows you how to generate the persistent code and describes how the Object Model maps to ORM-Persistent Class.

In this chapter:

Introduction

Persistent code is the object that exposes the ability to store and retrieve data in relational databases permanently. An instance of the persistence class represents a row of a database table. As the persistent code is generated based on the object models, a mapping between object models and relational models is automatically built, which in turn supports your software development in database applications in an easier way.

ORM-Persistable Class

You are provided with three ways to generate persistent code.

  1. Generating Persistent Code from ERD
  2. Generating Persistent Code from Class Diagram
  3. Generating Persistent Code from Database

Generating ORM-Persistable Class from Data Model

You can generate the persistent code from data model in one of the two ways:
  1. Using Database Code Generation Dialog Box
  2. Using ORM Wizard

Using Database Code Generation Dialog Box

As persistent code is generated based on the object model, and it will be used to manipulate the relational database which is the data model, if the object and data models were not synchronized, the generated persistent code may become malfunction.

Hence, there is a prerequisite of using Database Code Generation dialog box to generate persistent code, which is the synchronization between object and data models. The persistent code will be generated successfully as if the object and data models are synchronized in advance. For information on synchronization from data model to object model, refer to Synchronizing from a Data Model to an Object Model in the Object Model chapter.

To generate the persistent code from data model, synchronize the data model to object model before performing the following steps.

  1. On the menu, click Modeling > ORM > Generate Code....
  2. Figure 20.1 - To generate code

    The Database Code Generation dialog box is displayed.

    Figure 20.2 - Database Code Generation dialog
  3. Select the language of code to be generated from the drop-down menu of Language, either Java or C#.
  4. Figure 20.3 - Select programming code for generate
  5. Configure the code generation setting with respect to the selected language of code. Refer to the descriptions of Configuring Code Generation Setting for Java and Configuring Code Generation Setting for C# for information on how to configure the code generation setting for Java and C# respectively.
  6. Click OK, you will be prompted by the Generate ORM Code/Database dialog box showing the progress of database generation. Click Close when the generation is complete; whereas if generation is failed, you will be prompted by a Message dialog box informing you to find the error/warning message on the Message Pane.
  7. Figure 20.4 - Generate ORM Code/Database dialog

    The persistent code is generated.

Configuring Code Generation Setting for Java

By specifying the settings of code generation for Java, the Java persistence class will be generated accordingly.

Figure 20.5 - Generate code setting for Java
Configuring Code Generation Setting for C#

By specifying the settings of code generation for C#, the C# persistence class will be generated accordingly.

Figure 20.12 - Generate Code options for C#

Using ORM Wizard

For information on generating persistent code from data model by using wizard, refer to the descriptions of Generating Code and Database from ERD in the Using ORM Wizard chapter.

Generating ORM-Persistable Class from Object Model

You can generate the persistent code from object model in one of the two ways:

  1. Using Database Code Generation Dialog Box
  2. Using ORM Wizard

Using Database Code Generation Dialog Box

Persistent code is designed to manipulate the relational database. The persistent code maps to object model; and meanwhile, data model maps to relational database. Thus, synchronization between object model and data model is important such that the object-relational mapping environment can be built successfully.

Hence, synchronizing the object model to data model is the prerequisite of using Database Code Generation dialog box. For more information, refer to Synchronizing from an Object Model to a Data Model in the Data Model chapter.

For information about using Database Code Generation dialog box, refer to Using Database Code Generation Dialog Box section.

Using ORM Wizard

For information on generating persistent code from object model by using wizard, refer to the descriptions of Generating Code and Database from Class Diagram in the Using ORM Wizard chapter.

Generating ORM-Persistable Class from Database

You can generate the persistent code from database by using Wizard. For more information, refer to the descriptions of Generating Code from Database in the Using ORM Wizard chapter.

Mapping Object Model to ORM-Persistable Java Class

The persistent code can be generated with the support on mapping from Object Model to Persistent Code. In this section, examples will be given to show the mapping between object model and Java persistence class.

Mapping Classes, Attributes and Data Type

For each class element which may have attributes and methods in the object model, a corresponding Java class will be generated. The generated ORM persistable Java class will have the name as same as the name of the class element for identification. As if the class element has attributes with specified data types, all the attributes and data types will map to the Java class as well.

Example:

Figure 20.18 - Mapping the class model to code

In the above example, an ORM-Persistable Java class, Product was generated according to the class Product in the object model. There are three types of mapping shown above:

  1. Mapping Class Name
  2. The name of the class, Product maps to the name of the Java class, Product. (It is illustrated by the red line in the diagram.)

  3. Mapping Attribute
  4. The attributes of the class map to the attributes of the Java class. (It is illustrated by the green line in the diagram.) Besides, a pair of getter and setter methods for the corresponding attribute is generated to the Java class.

  5. Mapping Data Type
  6. The data type of the class maps to the data type of the Java Class. (It is illustrated by the blue line in the diagram.)

Mapping Data Type

The following table shows the data type mapping between object model and Java persistent code.

Object Model Java Persistent Code
Boolean boolean
Byte byte
Byte[] byte[]
Blob java.sql.Blob
Char char
Character character
String String
Int int
Integer Integer
Double int
Bigdecimal BigDecimal
Float float
Long long
Short short
Date java.util.Date
Time java.sql.Time
Timestamp java.sql.Timestamp
Table 20.1

Mapping Primary Key

As object model maps to data model bridging the environment between object model and relational database, primary key is mapped. As the object model maps to persistent code, primary key is in turn mapped.

Example:

Figure 20.19 - Mapping primary key

In the above example, the primary key of Product entity, ProductID maps to an attribute, ProductID of class element, Product, which in turn becomes an attribute of the Java class.

In the ORM-Persistable class, a pair of getter and setter methods for the attribute is generated for accessing its value. Besides, the ORM-Persistable class has a method named as getORMID which returns the primary key value of the class.

In the above example, the getORMID() method returns the value by calling the method getProductID() which returns the value of ProductID. It thus signifies ProductID is the primary key of the class.

Mapping Generalization

Generalization distributes the commonalities from the superclass among a group of subclasses. The subclass is consistent with the superclass and it may contain additional attributes and methods to make it specific.

Mapping generalization hierarchy to persistent code, the superclass and its subclasses are generated to individual Java class while the subclasses inherit all the superclass attributes and methods.

Example:

Figure 20.20 - Mapping generalization

In the above example, the generalization hierarchy of BankAccount, CheckingAccount and SavingAccount maps to three ORM-Persistable Java classes where the subclasses, CheckingAccount and SavingAccount extend their superclass, BankAccoount so as to inherit the BankAccount's attributes and methods.

Mapping Association and Multiplicity

An association specifies the semantic relationship between two classes in the object model with a role attached at one end. The role specifies the detailed behavior of a class associated in the association, including its role name, which indicates how the class acts in the association; and multiplicity, which specifies the number of links between each instance of the associated classes.

The role name of the association maps to an attribute which specifies how the class associates with its associated supplier class with respect to multiplicity. If a class has a multiplicity of many in an association, the class maps not only to an ORM persistent Java class, but also to a collection class which supports manipulating the objects within a collection.

You are allowed to define the type of collection class for code generation. Meanwhile, List and Set are the default types for ordered and un-ordered collection respectively.

Example:

Figure 20.21 - Classes with one-to-many association

In the above object model, there is a one-to-many association between Customer class and PurchaseOrder class; defining that a customer can place many purchase order meanwhile a purchase order is placed by one customer. As object model maps to persistent code, three ORM-Persistable classes will be generated, including Customer, PurchaseOrder and PurchaseOrderSetCollection classes.

Figure 20.22 - Mapping class with multiplicity of one

From the above diagram, PurchaseOrder class has the role of "PlacedBy" in the association and it is associated with the Customer class with multiplicity of one. The role, "PlacedBy" maps as an attribute to the PurchaseOrder class which is defined as the data type of Customer class; it implies that the attribute "PlacedBy" stores a customer object which places the particular purchase order.

Figure 20.23 - Mapping class has the multiplicity of many

From the above diagram, Customer has the role of "Places" in the association which associates with many PurchaseOrder instances. As the PurchaseOrder class has the multiplicity of many in the association, a collection class is generated so as to manipulate the multiple instances of PurchaseOrder. The role, "Places" maps as an attribute to the Customer class with the data type of PurchaseOrderSetCollection class; it implies that the attribute "Places" represents a collection of PurchaseOrder objects which was placed by a particular customer.

Mapping Navigable Association

An association, which specifies a semantic relationship occurring between typed instances, has at least two ends represented by properties. When a property at an end of the association is owned by an end class, it represents the association is navigable from the opposite ends. On the contrary, when a property does not appear in the namespace of any associated class, it represents a non-navigable end of the association.

An open arrowhead on the end of an association indicates the end is navigable. Meanwhile, a small cross (x) on the end of an association indicates the end is not navigable. If an association has two navigable ends, it is considered as bi-directional associations.

As the role name of the association maps to an attribute which specifies how the class associates with its associated class, however if the association has a non-navigable end, the corresponding role name will not map to an attribute, suppressing the ownership of the class.

Example:

Figure 20.24 - Classes with Navigable Association

In the above object model, it shows a binary association with one end navigable and the other non-navigable. It indicates that Product is owned by OrderLine while Product cannot own OrderLine. As the object model maps to persistent code, the ownership of OrderLine instance in Product class will not be generated in the persistent code.

Figure 20.25 - Mapping Navigable Association

From the above diagram, OrderLine class has the role of "Orders" in the association and associated with the one Product instance. The role, "Orders" maps as an attribute to OrderLine class which defines as the data type of Product; it implies that the attribute "Orders" stores a product instance.

On the other hand, Product class has the role of "OrderedBy" associated with many OrderLine instances in a non-navigable association; that is, Product class does not have the ownership of OrderLine. Even though the OrderLine class has the multiplicity of many in the association, the corresponding collection class of OrderLine for the Product class is not generated to manipulate it.

Mapping Object Model to ORM-Persistable .NET Class

As the .NET persistence class can be generated with the support on mapping from Object Model to Persistent Code. In this section, examples will be given to show the mapping between object model and .Net persistence class.

Mapping Classes, Attributes and Data Type

For each class element which may have attributes and methods in the object model, a corresponding persistence class of C# will be generated. The generated ORM persistable .NET class will have the name as same as the name of the class element for identification. As if the class element has attributes with specified data types, all the attributes and data types will map to the .NET persistence class as well.

Example:

Figure 20.26 - Mapping Classes, Attributes and Data Types

In the above example, an ORM-Persistable .NET class, Product was generated according to the class Product in the object model. There are three types of mapping shown above:

  1. Mapping Class Name
  2. The name of the class, Product maps to the name of the .NET persistence class, Product. (It is illustrated by the red line in the diagram.)

  3. Mapping Attribute
  4. The attributes of the class map to the attributes of the .NET persistence class. (It is illustrated by the green line in the diagram.) Besides, a pair of getter and setter methods for the corresponding attribute is generated to the persistence class.

  5. Mapping Data Type
  6. The data type of the class maps to the data type of the .NET persistence class. (It is illustrated by the blue line in the diagram.)

Mapping Data Type

The following table shows the data type mapping between object model and .NET persistent code.

Object Model .NET Persistent Code
Boolean bool
Byte byte
Byte[] byte[]
Blob byte[]
Char char
Character char
String string
Int int
Integer int
Double double
Decimal Decimal
Float float
Long long
Short short
Date DateTime
Time DateTime
Timestamp DateTime
Table 20.2

Mapping Primary Key

As object model maps to data model bridging the environment between object model and relational database, primary key is mapped. As the object model maps to persistent code, primary key is in turn mapped.

Example:

Figure 20.27 - Mapping primary key

In the above example, the primary key of Product entity, ProductID maps to an attribute, ProductID of class element, Product, which in turn becomes an attribute of the .NET persistence class.

In the ORM-Persistable .NET class, a pair of getter and setter methods for the attribute is generated for accessing its value. Besides, the ORM-Persistable .NET class has a method of getting the ORMID which returns the primary key value of the class.

In the above example, the getter method of ORMID returns the value by calling the getter method of ProductID which returns the value of __ProductID. It thus signifies ProductID is the primary key of the class.

Mapping Generalization

Generalization distributes the commonalities from the superclass among a group of subclasses. The subclass is consistent with the superclass and it may contain additional attributes and methods to make it specific.

Mapping generalization hierarchy to persistent code, the superclass and its subclasses are generated to individual .NET persistence class while the subclasses inherit all the superclass attributes and methods.

Example:

Figure 20.28 - Mapping generalization

In the above example, the generalization hierarchy of BankAccount, CheckingAccount and SavingAccount maps to three ORM-Persistable .NET classes where the subclasses, CheckingAccount and SavingAccount extend their superclass, BankAccoount so as to inherit the BankAccount's attributes and methods.

Mapping Association and Multiplicity

An association specifies the semantic relationship between two classes in the object model with a role attached at one end. The role specifies the detailed behavior of a class associated in the association, including its role name, which indicates how the class acts in the association; and multiplicity, which specifies the number of links between each instance of the associated classes.

The role name of the association maps to an attribute which specifies how the class associates with its associated supplier class with respect to multiplicity. If a class has a multiplicity of many in an association, the class maps not only to an ORM persistent .NET class, but also to a collection class which supports manipulating the objects within a collection.

You are allowed to define the type of collection class for code generation. Meanwhile, List and Set are the default types for ordered and un-ordered collection respectively.

Example:

Figure 20.29 - Classes with one-to-many relationship

In the above object model, there is a one-to-many association between Customer class and PurchaseOrder class; defining that a customer can place many purchase order meanwhile a purchase order is placed by one customer. As object model maps to persistent code, three ORM-Persistable .NET classes will be generated, including Customer, PurchaseOrder and PurchaseOrderSetCollection classes.

Figure 20.30 - Mapping class with multiplicity of one

From the above diagram, PurchaseOrder class has the role of "PlacedBy" in the association and it is associated with the Customer class with multiplicity of one. The role, "PlacedBy" maps as an attribute to the PurchaseOrder class which is defined as the data type of Customer class; it implies that the attribute "PlacedBy" stores a customer object which places the particular purchase order.

Figure 20.31 - Mapping class has the multiplicity of many in the association

From the above diagram, Customer has the role of "Places" in the association which associates with many PurchaseOrder instances. As the PurchaseOrder class has the multiplicity of many in the association, a collection class is generated so as to manipulate the multiple instances of PurchaseOrder. The role, "Places" maps as an attribute to the Customer class with the data type of PurchaseOrderSetCollection class; it implies that the attribute "Places" represents a collection of PurchaseOrder objects which was placed by a particular customer.

Mapping Navigable Association

An association, which specifies a semantic relationship occurring between typed instances, has at least two ends represented by properties. When a property at an end of the association is owned by an end class, it represents the association is navigable from the opposite ends. On the contrary, when a property does not appear in the namespace of any associated class, it represents a non-navigable end of the association.

An open arrowhead on the end of an association indicates the end is navigable. Meanwhile, a small cross (x) on the end of an association indicates the end is not navigable. If an association has two navigable ends, it is considered as bi-directional associations.

As the role name of the association maps to an attribute which specifies how the class associates with its associated class, however if the association has a non-navigable end, the corresponding role name will not map to an attribute, suppressing the ownership of the class.

Example:

Figure 20.32 - Classes with Navigable Association

In the above object model, it shows a binary association with one end navigable and the other non-navigable. It indicates that Product is owned by OrderLine while Product cannot own OrderLine. As the object model maps to persistent code, the ownership of OrderLine instance in Product class will not be generated in the persistent code.

From the above diagram, OrderLine class has the role of "Orders" in the association and associated with the one Product instance. The role, "Orders" maps as an attribute to OrderLine class which defines as the data type of Product; it implies that the attribute "Orders" stores a product instance.

On the other hand, Product class has the role of "OrderedBy" associated with many OrderLine instances in a non-navigable association; that is, Product class does not have the ownership of OrderLine. Even though the OrderLine class has the multiplicity of many in the association, the corresponding collection class of OrderLine for the Product class is not generated to manipulate it.
Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com