21.07.2013 Views

Chapter 16 - Using ORM Wizard - Visual Paradigm

Chapter 16 - Using ORM Wizard - Visual Paradigm

Chapter 16 - Using ORM Wizard - Visual Paradigm

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>16</strong><br />

<strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong>


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>Chapter</strong> <strong>16</strong> - <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

An <strong>ORM</strong> <strong>Wizard</strong> is provided to you to generate persistent code and/or database either from database, class diagram or entity<br />

relationship diagrams. This chapter shows you how to activate the <strong>ORM</strong> <strong>Wizard</strong> and generate code and/or database by one of<br />

the three options provided by wizard.<br />

In this chapter:<br />

<strong>16</strong>-2<br />

• Introduction<br />

• Generating Code from Database<br />

• Generating Code and Database from ERD<br />

• Generating Code and Database from Class Diagram<br />

Introduction<br />

Mapping objects to relational database is a complicated and error pound task in the development. Acting as a bridge between<br />

object model, data model and relational database, the mappings between these models are automated. Apart from these<br />

mappings, the mapping between object models and persistent code is also supported. Hence, the persistent code can thus map<br />

to the relational database.<br />

The synchronization between persistent code, object model, data model and relational is supported, which reduces the<br />

development time for handling these tedious programming jobs between them. Moreover, your document will always keep upto-date.<br />

To support synchronization in-between persistent code and relational database, you are allowed to generate database<br />

and persistent code for your development project.<br />

An <strong>ORM</strong> <strong>Wizard</strong> for the generation of persistent code and database. The wizard provides you with three options:<br />

1. Generate Code from Database.<br />

2. Generate Code and Database from Entity Relationship Diagram (ERD).<br />

3. Generate Code and Database from Class Diagram.<br />

To activate the <strong>Wizard</strong>:<br />

1. On the menu, click Modeling > <strong>ORM</strong> > <strong>Wizard</strong>s....<br />

Figure <strong>16</strong>.1 - To open the <strong>ORM</strong> <strong>Wizard</strong>


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

2. A <strong>Wizard</strong> Welcome Page will be shown, select Language of the code to be generated from the drop-down menu,<br />

either Java or C#.<br />

Figure <strong>16</strong>.2 - Select the programming language<br />

3. Select one of the wizard options, and then click Next to proceed.<br />

Figure <strong>16</strong>.3 - Select the generation options<br />

<strong>16</strong>-3


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

Generating Code from Database<br />

<strong>16</strong>-4<br />

Figure <strong>16</strong>.4 - The flow of generate code<br />

Upon selecting the option for Generate Code from Database, the wizard helps you generate persistent code from database<br />

tables according to the requirements you specified throughout the wizard.<br />

Follow the steps of the Generate Code From Database wizard:<br />

1. Database Configuration<br />

• For Java Language selected<br />

Figure <strong>16</strong>.5 - Database configuration for Java<br />

You are asked to define the database configuration. Refer to the descriptions in the Getting Started with<br />

Object-Relational Mapping chapter for information chapter for information on how to configure the database<br />

Database Configuration for Java Project section.


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

• For C# Language selected<br />

2. Selecting Tables<br />

Figure <strong>16</strong>.6 - Database Configuration for C#<br />

You are asked to define the database configuration. Refer to the descriptions in the Getting Started with<br />

Object-Relational Mapping chapter for information on how to configure the database in the Database<br />

Configuration for .Net Project section.<br />

Figure <strong>16</strong>.7 - Select the Tables for generate code<br />

<strong>16</strong>-5


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-6<br />

The database is connected based on your options in the previous database configuration option pane and all database<br />

tables are reversed. You are asked to select the database tables which you want to generate persistent class to<br />

manipulate those tables. By default, all the database tables are selected for the generation of code shown in the list of<br />

Selected Tables. You can deselect the table by using the list of buttons between the list of Available Tables and<br />

Selected Tables.<br />

• Add Selected<br />

Add the selected table from Available Tables to Selected Tables.<br />

• Remove Selected<br />

Remove the selected table from Selected Tables to Available Tables.<br />

• Add All<br />

Add all tables from Available Tables to Selected Tables.<br />

• Remove All<br />

Remove all tables from Selected Tables to Available Tables.<br />

3. Class Details Configuration<br />

Figure <strong>16</strong>.8 - Configure the Class mapping<br />

After selecting tables, you will be directed to a Class Details Configuration pane. You are asked to define the Class<br />

Detail for generating code. The persistent classes will be generated based on the information defined here. You can<br />

edit the class details by double-clicking the field.


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

• Package<br />

Enter the package name. A package will be created to store the generated persistent code. If the package<br />

name was not defined, you will be prompted by a dialog box warning you the classes will be generated in<br />

default package.<br />

Figure <strong>16</strong>.9 - Confirm generate class in default package message<br />

• Class<br />

You can edit the class name which will be used as the name of the generated persistent code for a<br />

corresponding table.<br />

• Associations<br />

Figure <strong>16</strong>.10 - Class mapping<br />

You can edit the role name for a reference in the class.<br />

Figure <strong>16</strong>.11 - Association mapping<br />

You can deselect navigable for an association such that the reference for the target role will not be created.<br />

• Attributes<br />

Figure <strong>16</strong>.12 - Set the navigable<br />

You can edit the attribute name representing the column of the table.<br />

Figure <strong>16</strong>.13 - Attributes mapping<br />

<strong>16</strong>-7


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-8<br />

• Custom Code Style<br />

Click button, Custom Code Style Setting dialog box will be displayed. You can modify<br />

the prefix or suffix of the Class, Attribute and Role Name.<br />

Figure <strong>16</strong>.14 – Custom Code Style Setting dialog<br />

For the Type, select the type of Class detail, either Class, Attribute or Role Name (PK) that you want to<br />

apply code style.<br />

For the Prefix/Suffix, select either Prefix or Suffix to be added or removed.<br />

For the Add/Remove option, select the option for the action of code style to be applied.<br />

For the Textbox, enter the word for either prefix or suffix.<br />

For the Scope, select the scope of the code style to be applied to, either All or Selected.<br />

Table shows the result of applying Code Style.<br />

Code Style Before Applying After Applying<br />

Add Prefix (E.g. pre_) Item pre_Item<br />

Remove Prefix (E.g. pre_) pre_Item Item<br />

Add Suffix (E.g. _suf) Item Item_suf<br />

Remove (E.g. _suf) Item_suf Item<br />

4. Generate Code<br />

• For Java Language selected<br />

Table <strong>16</strong>.1<br />

Figure <strong>16</strong>.15 - Generate code setting for Java<br />

You are asked to specify the code details. For information, refer to the description of Configuring Code<br />

Generation Setting for Java section in the Implementation chapter.


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

• For C# Language selected<br />

Figure <strong>16</strong>.<strong>16</strong> - Generate code setting for C#<br />

You are asked to specify the code details. For information, refer to the description of Configuring Code<br />

Generation Setting for C# section in the Implementation chapter.<br />

5. Click Finish, the Generate <strong>ORM</strong> Code/Database dialog box appears showing the progress of code generation. Click<br />

Close when the generation is complete.<br />

Figure <strong>16</strong>.17 - Generate <strong>ORM</strong> Code/Database dialog<br />

A class diagram and an entity relationship diagram will be generated automatically and added to your project. The<br />

generated persistent code and required resources will be generated to the specified output path.<br />

<strong>16</strong>-9


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

Generating Code and Database From ERD<br />

Figure <strong>16</strong>.18 - Generating Code and Database From ERD<br />

Upon selecting the option for Generate Code and Database from ERD, the wizard helps you generate persistent code and<br />

database from ERD with respect to the requirements you specified throughout the wizard.<br />

Take the following ERD as an example:<br />

Figure <strong>16</strong>.19 - Entities Relationship Diagram<br />

Follow the steps of the Generate Code and Database From ERD wizard:<br />

<strong>16</strong>-10<br />

1. Class Details Configuration<br />

Figure <strong>16</strong>.20 - Class Detail Configuration


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

You are asked to define the Class Details for generating code. The persistent classes will be generated based on the<br />

information defined here. You can edit the class details by double-clicking the field.<br />

• Package<br />

Enter the package name. A package will be created to store the generated persistent code. If the package<br />

name was not defined, you will be prompted by a dialog box warning you the classes will be generated in<br />

default package.<br />

Figure <strong>16</strong>.21 - Confirm generate code in default package message<br />

• Class<br />

You can edit the class name which will be used as the name of the generated persistent code for a<br />

corresponding table.<br />

• Associations<br />

Figure <strong>16</strong>.22 - Mapping classes<br />

You can edit the role name for a reference in the class.<br />

Figure <strong>16</strong>.23 - Mapping associations<br />

You can deselect navigable for an association such that the reference for the target role will not be created.<br />

• Attributes<br />

Figure <strong>16</strong>.24 - Set the navigable<br />

You can edit the attribute name representing the column of the table.<br />

Figure <strong>16</strong>.25 - Mapping attributes<br />

<strong>16</strong>-11


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-12<br />

• Custom Code Style<br />

Click button, Custom Code Style Setting dialog box will be displayed. You can modify<br />

the prefix or suffix of the Class, Attribute and Role Name.<br />

Figure <strong>16</strong>.26 - Custom Code Style Setting dialog<br />

For the Type, select the type of Class detail, either Class, Attribute or Role Name (PK) that you want to<br />

apply code style.<br />

For the Prefix/Suffix, select either Prefix or Suffix to be added or removed.<br />

For the Add/Remove option, select the option for the action of code style to be applied.<br />

For the Textbox, enter the word for either prefix or suffix.<br />

For the Scope, select the scope of the code style to be applied to, either All or Selected.<br />

Table shows the result of applying Code Style.<br />

Code Style Before Applying After Applying<br />

Add Prefix (E.g. pre_) Item pre_Item<br />

Remove Prefix (E.g. pre_) pre_Item Item<br />

Add Suffix (E.g. _suf) Item Item_suf<br />

Remove (E.g. _suf) Item_suf Item<br />

2. Database Configuration<br />

• For Java Language selected<br />

Table <strong>16</strong>.2<br />

Figure <strong>16</strong>.27 - Database Configuration for Java


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

You are asked to define the database configuration. Refer to the descriptions in the Getting Started with<br />

Object-Relational Mapping chapter for information on how to configure the database in the Database<br />

Configuration for Java Project section<br />

• For C# Language selected<br />

Figure <strong>16</strong>.28 - Database configuration for C#<br />

You are asked to define the database configuration. Refer to the descriptions in the Getting Started with<br />

Object-Relational Mapping chapter for information on how to configure the database in the Database<br />

Configuration for .Net Project section.<br />

3. Generate Code<br />

• For Java Language selected<br />

Figure <strong>16</strong>.29 - Generate Code Configure for Java<br />

You are asked to specify the code details. For information, refer to the description of Configuring Code<br />

Generation Setting for Java section in the Implementation chapter.<br />

<strong>16</strong>-13


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-14<br />

• For C# Language selected<br />

Figure <strong>16</strong>.29 - Generate Code configure for C#<br />

You are asked to specify the code details. For information, refer to the description of Configuring Code<br />

Generation Setting for C# section in the Implementation chapter.<br />

4. Click Finish, the Generate <strong>ORM</strong> Code/Database dialog box appears showing the progress of code generation. Click<br />

Close when the generation is complete.<br />

Figure <strong>16</strong>.30 - Generate <strong>ORM</strong> Code/Database dialog<br />

A class diagram will be generated automatically and added to your project. The generated persistent code and required<br />

resources will be generated to the specified output path and the generated database will be set up to the specified<br />

database configuration.


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

Generating Code and Database from Class Diagram<br />

Figure <strong>16</strong>.31 - Generating Code and Database from Class Diagram<br />

Upon selecting the option for Generate Code from Class Diagram, the wizard helps you generate persistent code and database<br />

from class diagram with respect to the requirements you specified throughout the wizard.<br />

Take the following class diagram as an example:<br />

Figure <strong>16</strong>.32 - Class Diagram<br />

Follow the steps of the Generate Code and Database from Class Diagram wizard:<br />

1. Selecting Classes<br />

Figure <strong>16</strong>.33 - Select Persistable Classes<br />

<strong>16</strong>-15


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-<strong>16</strong><br />

You are asked to select the classes on the class diagram which you want to generate persistent class to manipulate<br />

persistent data. By default, all the classes stereotyped as <strong>ORM</strong>-Persistable on the class diagram are selected for the<br />

generation of code and database shown in the list of Persistable Classes. You can deselect the persistable classes by<br />

using the list of buttons between the list of Non Persistable Classes and Persistable Classes.<br />

• Add Selected<br />

Add the selected class from Non Persistable Classes to Persistable Classes.<br />

• Remove Selected<br />

Remove the selected class from Persistable Classes to Non Persistable Classes.<br />

• Add All<br />

Add all classes from Non Persistable Classes to Persistable Classes.<br />

• Remove All<br />

Remove all classes from Persistable Classes to Non Persistable Classes.<br />

2. Select Primary Key<br />

For the classes shown in the list of Persistable Classes, they will be stereotyped as <strong>ORM</strong>-<br />

Persistable on the class diagram after the wizard is finished. Meanwhile, for the classes shown<br />

in the list of Non Persistable Classes, they will not be stereotyped on the class diagram after<br />

the wizard is finished.<br />

Figure <strong>16</strong>.34 - Select primary key for each table<br />

You are asked to select the primary key for each class being mapped to data model and relational database. You can<br />

either select an attribute as the primary key or let SDE-VS generate the primary key automatically by using the dropdown<br />

menu. For more information, refer to the description of Mapping Primary key in the section of Mapping an<br />

Object Model to a Data Model in the Object Model chapter.


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

3. Table Details Configuration<br />

Figure <strong>16</strong>.35 - Table details configuration<br />

You are asked to define the Table Details for generating database and code. The database and persistent classes will<br />

be generated based on the information defined here. You can edit the table details by double-clicking the field.<br />

• Package<br />

Enter the package name. A package will be created to store the generated persistent code. If the package<br />

name was not defined, you will be prompted by a dialog box warning you the classes will be generated in<br />

default package.<br />

Figure <strong>16</strong>.36 - Confirm generate classes in default package message<br />

• Table<br />

You can edit the table name which will be used as the name of the generated database table.<br />

• Columns<br />

You can edit the column name represented by the class.<br />

Figure <strong>16</strong>.37 - Columns mapping<br />

<strong>16</strong>-17


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-18<br />

• Custom Code Style<br />

Click button, Custom Code Style Setting dialog box will be displayed. You can modify<br />

the prefix or suffix of the Table, Column, Primary Key and Foreign Key.<br />

Figure <strong>16</strong>.38 - Custom Code Style Setting dialog<br />

For the Type, select the type of Table detail, either Table, Column, Primary Key or Foreign Key that you<br />

want to apply code style.<br />

For the Prefix/Suffix, select either Prefix or Suffix to be added or removed.<br />

For the Add/Remove option, select the option for the action of code style to be applied.<br />

For the Textbox, enter the word for either prefix or suffix.<br />

For the Scope, select the scope of the code style to be applied to, either All or Selected.<br />

Table shows the result of applying Code Style.<br />

Code Style Before Applying After Applying<br />

Add Prefix (E.g. pre_) Item pre_Item<br />

Remove Prefix (E.g. pre_) pre_Item Item<br />

Add Suffix (E.g. _suf) Item Item_suf<br />

Remove (E.g. _suf) Item_suf Item<br />

4. Database Configuration<br />

• For Java Language selected<br />

Table <strong>16</strong>.3<br />

Figure <strong>16</strong>.39 - Database configuration for Java


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

You are asked to define the database configuration. Refer to the descriptions in the Getting Started with<br />

Object-Relational Mapping chapter for information on how to configure the database in the Database<br />

Configuration for Java Project section<br />

• For C# Language selected<br />

Figure <strong>16</strong>.40 - Database Configuration for C#<br />

You are asked to define the database configuration. Refer to the descriptions in the Getting Started with<br />

Object-Relational Mapping chapter for information on how to configure the database in the Database<br />

Configuration for .Net Project section.<br />

5. Generate Code<br />

• For Java Language selected<br />

Figure <strong>16</strong>.41 - Generate code configuration for Java<br />

You are asked to specify the code details. For information, refer to the description of Configuring Code<br />

Generation Setting for Java section in the Implementation chapter.<br />

<strong>16</strong>-19


SDE-VS User’s Guide (Part 2, for <strong>ORM</strong>) <strong>Chapter</strong> <strong>16</strong> – <strong>Using</strong> <strong>ORM</strong> <strong>Wizard</strong><br />

<strong>16</strong>-20<br />

• For C# Language selected<br />

Figure <strong>16</strong>.42 - Generate code configuration for C#<br />

You are asked to specify the code details. For information, refer to the description of Configuring Code<br />

Generation Setting for C# section in the Implementation chapter.<br />

<strong>Wizard</strong> for Generate Code and Database from Class Diagram option provides an option of<br />

generating code to you. By default, the Generate Code option is selected. If you do not want to<br />

generate code from class diagram, please deselect the Generate Code option. In this case, only<br />

database will be generated while persistent code will not be generated.<br />

Click Finish, the Generate <strong>ORM</strong> Code/Database dialog box appears showing the progress of code<br />

generation. Click Close when the generation is complete.<br />

Figure <strong>16</strong>.43 - Generate <strong>ORM</strong> Code/Database dialog<br />

An entity relationship diagram will be generated automatically and added to your project. The generated persistent<br />

code and required resources will be generated to the specified output path and the generated database will be set up to<br />

the specified database configuration.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!