Posts tagged UML
Generating Database Objects from an UML Model – Part 1
Jun 5th
In this mini-series of one post, I’ll show you how to generate SQL-script from an UML model.
First, you need a model. This can be any model, but here I use an UML model. The class diagram on the left shows a simple model consisting of a parent class and a related child class. Both are stereotyped <<table>> so you can see we are talking about a data model.
Both have some attributes. Note that their types have been declared explicitly.
The interesting part is their association. As you can see it is a traditional master-detail relationship. For clarity I have named the association and both association ends, but the latter is not strictly necessary.
This UML diagram can be represented in the following XML form:
<?xml version="1.0" encoding="utf-8"?>
<EntityModel>
<Entity Name="TestParent">
<Property Name="TestParentName1" Type="String" />
<Property Name="TestParentNumber" Type="Integer" />
</Entity>
<Entity Name="TestChild">
<Property Name="TestChildName" Type="String" />
</Entity>
<Association Name="FK_TestParent_TestChild">
<End Name="TestParent_End" Role="TestParent" Multiplicity="0..1" />
<End Name="TestChild_End" Role="TestChild" Multiplicity="*" />
</Association>
</EntityModel>
Why that is interesting? That is because XML easily can be transformed into something useful.
For performing the actual transformation you can use, for example:
- XSLT stylesheet-driven transformation
- template-driven code generator
- code using XmlSerializer or XmlReader
Thus, it is transformed into a sql-script.
/****** Table [dbo].[TestParent] ******/ CREATE TABLE [dbo].[TestParent]( [TestParentID] [int] IDENTITY(1,1) NOT NULL, [TestParentName1] [varchar](MAX) NULL, [TestParentNumber] [int] NULL, CONSTRAINT [PK_ParentTable] PRIMARY KEY CLUSTERED ([TestParentID] ASC) ) GO /****** Table [dbo].[TestChild] ******/ CREATE TABLE [dbo].[TestChild]( [TestChildID] [int] IDENTITY(1,1) NOT NULL, [TestParentID] [int] NULL, [TestChildName] [varchar](50) NULL, CONSTRAINT [PK_ChildTable] PRIMARY KEY CLUSTERED ([TestChildID] ASC) ) GO /****** ForeignKey [FK_TestParent_TestChild] ******/ ALTER TABLE [dbo].[TestChild] ADD CONSTRAINT [FK_TestParent_TestChild] FOREIGN KEY([TestParentID]) REFERENCES [dbo].[TestParent] ([TestParentID]) GO
The keen eye will notice that in the final sql-script primary and foreign key columns are introduced, which are not present in the original model (diagram nor xml file). The other way around, specification of associations in the original model is much richer than the resulting foreign key constraint in the sql-script. These “mismatches” must be handled and are typically present when performing O/RM mapping.
Extracting Business Knowledge From Data Models
Mar 4th
Recently, I was following a discussion on extracting business knowledge from UML models. UML models, UML models, and more UML models.
Slowly a thought was emerging from my soaky brain:
hey dude, data models are models too.
In fact, a data model doesn’t always have to be represented by an ERD diagram.
It is also very feasible to diagram a data model using UML class diagrams. It even gets better when you stereotype your classes <<table>>. This enables code generation software to automagically recognize the stereotypes and – for example – generate SQL script for you.
Why on earth that could be remotely interesting?
Because your model is independent from its physical representation.
This way, you can “extract” a model from the physical database, introduce some enhancements, and re-apply the enhancements to the original physical database.
It enables you to enforce an uniform use of naming conventions.
Conceptual and Storage Model
Dec 27th
The most common model is the data model. Deep in the application architecture a physical model can be found, which is a straight representation of the database (ERD diagram).
More superficially, in the core of the application architecture, a conceptual representation of the same data model can be found (UML diagram). However, it should be kept in mind that both are derived from the same underlying data model, thus are essentially the same. As a result mapping from one to another is relatively straight forward (Data Source Explorer).
The Art of Creating A Top model
Jul 15th
If you want to master the art of model-driven code generation, an essential thing you need is a model of some kind. Maybe you are in desperate need of Kate Moss, but that’s not the kind of model I talking about. I mean some kind of data model.
Preferably, you describe your model in UML. But that is not essential.
As far as I am concerned, you can write it down in notepad, if you like.
My model contains students. Students are member of a certain class, for example a class named Class 3a. A typical class consists of many students, but a student can only be part of one class. The students attend many different courses and a course can have many students.
This data model translates to database tables and relations. In this case you have a table Student, Class and Course. Student has a 1-to-many relationship with Class and a many-to-many relationship with Course.
If you created this database and then extracted a database model from the database, it would look like this:
This would be a physical data model. A drawback of a physical model is that there is no proper way to show a many-to-many relationship. A join table StudentCourse is needed to show such a relationship. In fact it is a pattern to create a many-to-many join in a table.
In UML it looks like this:
And this is really all there is to create an UML class diagram.












