Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 18: Rose Data Modeler

and column selections on the other tabs.

Generating an Object Model from a Data Model

One of the new features of Rose is the ability to automatically generate an object model from a data model. This feature is particularly useful when you are working on a project to re−engineer an existing application and database. You can reverse engineer an existing database, and then generate the object model from it. Of course, this feature is useful in other types of projects as well. Any time you want to be sure that the object model and data model are consistent, or you want to reverse engineer information from an existing database or data model, the object model generation is helpful.

Not all of the constructs in the data model have meaning in an object model. Indexes, stored procedure, and other database elements are not mapped to the object model. Table 18.3 lists the data model elements and the corresponding object model elements.

Table 18.3: Data Model and Object Model Elements

Data Model Element

Object Model Element

Schema

Package

Table

Class

Column

Attribute

Trigger

None

Stored procedure

None

Intersection table with only primary/foreign key

Many−to−many association

columns

 

Intersection table with columns other than

Many−to−many association with association class

primary/foreign key

 

Identifying relationship

Composite aggregation

Non−identifying relationship

Association

Cardinality

Cardinality

Index

None

Database

None

Constraint

None

Domain

None

To create an object model from a data model:

 

1.

Right−click the schema and select Data Modeler → Transform to Object Model.

2.

Enter the destination package name. The destination package is the name of a package that will be created in the Logical view to hold the new objects.

3.

648

Chapter 18: Rose Data Modeler

Enter the prefix. The prefix will be added to the name of each table to create the classes in the object model.

4.

Select the Include Primary Keys check box to create attributes for the primary key columns as well as for the other columns. If this check box is not selected, Rose will generate attributes for the non−primary key columns, but not for the primary key columns.

Generating a Data Model from an Object Model

Just as you can generate an object model from a data model, you can generate a data model from an object model. As the project progresses and you have discovered more entity classes, generating the data model will give you a good start to your database design.

When generating the data model, Rose will look for classes with the persistent attribute marked as True. You can set a class to Persistent or Transient in the standard class specification window on the Detail tab. If you want to generate a table for the class, set it to Persistent.

A package of classes in the Logical view will become a schema in the data model. If there is an existing schema of the same name, Rose will add any new classes as tables in the schema. It will not, however, change the tables in the schema if classes in the object model change. Instead, it will note the changes in a log so that you can apply them to the schema if you wish.

Table 18.4 lists object model elements and their corresponding data model elements.

649

Chapter 18: Rose Data Modeler

Table 18.4: Object Model and Data Model Elements

 

 

 

Object Model Element

Data Model Element

Package

Schema

Persistent class

Table

Attribute

Column

Operation

None

Many−to−many association

Intersection table

Composite aggregation

Identifying relationship

Association or non−composite aggregation

Non−identifying relationship

Cardinality

Cardinality

Association class

Intersection table

To create a data model from an object model:

 

1.

Create a database in the Component view.

2.

Right−click any attribute in the classes that you wish to become a primary key in the generated tables. Select Data Modeler Part of Object Identity. If you do not select a primary key attribute, Rose will create a primary key for you by creating a column called "<table name>ID."

3.

Right−click a package in the Logical view and select Data Modeler → Transform to Data Model.

4.

Enter the destination schema name, which is the name of a schema that will be created to hold the new data elements.

5.

Enter the target database, which is the name of an existing database in the Component view.

6.

Enter a prefix that will be added to each class name to create the table names.

7.

Select the Create Indexes for Foreign Keys check box to automatically create index constraints for foreign keys.

650

Chapter 18: Rose Data Modeler

Generating a Database from a Data Model

At any point during the project, you can generate the database or DDL script from the data model. Rose gives you the choice of simply generating the DDL or running the DDL to create the database.

Rose includes a wizard that walks you through the steps of creating the database. To begin, right−click a schema to generate and select Data Modeler → Forward Engineer. After the welcome screen, select the elements you wish to generate:

651

Chapter 18: Rose Data Modeler

Next, enter the name of the DDL file to create. If you want to create the DDL, but not run it, click Next. If you want to run the new DDL against a database, select the Execute check box. Enter the connection information for your DBMS and press the Test Connection button to be sure that the connection is working properly.

Press Finish to complete the process. Rose will generate the DDL and optionally run it against the database. If any errors are encountered, Rose will add them to the log.

All of the tables, columns, and relationships in the schema will be generated in the DDL or database. The following example shows a table in the Rose model.

The following is the corresponding DDL:

CREATE TABLE T_Customer (

CUSTOMER_ID SMALLINT IDENTITY NOT NULL, FIRST_NAME VARCHAR ( 15 ) NOT NULL, LAST_NAME VARCHAR ( 15 ) NOT NULL, HOME_PHONE NUMERIC ( 10 ) NOT NULL, ADDRESS VARCHAR ( 20 ) NOT NULL,

CITY VARCHAR ( 20 ) NOT NULL, STATE CHAR ( 2 ) NOT NULL, ZIP_CODE NUMERIC ( 5 ) NOT NULL,

CONSTRAINT PK_T_Customer0 PRIMARY KEY NONCLUSTERED (CUSTOMER_ID), CONSTRAINT TC_T_Customer1 CHECK (CUSTOMER_ID > 1000)

652