2

I am starting a whole project by myself for the first time, and I am stuck between the UML modelization ( Class diagram ) and the database structure.

Should I use the exact same classes that I model in the class diagram in the database?

For example, I have two User, the Service_provider and the Client. In the class diagram I consider each one of them as a unique class. But I chose to use single table inheritance in the database, so I store both users in the same table and add the role attribute to it.

Does my modelization stay valid in this case? or I just need one class for User? (but in this case, I can't show the relationship between the Service_provider and the Client as the Service_provider could have many Clients)

Could anyone explain this to me, please?

here are my thoughts for the class diagram

Christophe
  • 68,716
  • 7
  • 72
  • 138
Elin
  • 105
  • 1
  • 7
  • Hi @Ellin, your single table approach is also called *Table-Per-Hierarchy (TPH)*, but you can also use approaches with multiple tables. [This post](https://stackoverflow.com/questions/190296/how-do-you-effectively-model-inheritance-in-a-database) has a good discussion about it and I believe it will answer your question. My personal suggestion would be to create multiple tables, as it is difficult for me to imagine a class like Client not ending with unique members that do not exist in User, a scenario that would result in few nullable columns in the database. (if using a single table). – diogoslima Feb 25 '20 at 13:59
  • Hello, @diogoslima, Actually I am choosing to use the single table approach because I just have one field that exists in the service_provider and not in the client table, and all the other fields are the same, so I didn't want repeat two tables that have almost the exact same fields. – Elin Feb 25 '20 at 15:06
  • Great @Elin, so if you made this decision, I believe the short answer to your question is that your model is valid and fine. Usually, best practices guide us that a relational database and an application have independent designs, as they are built on top of different requirements. If you know your options (as I shared in the previous comment) and made your decision, it looks good Let me know if you want me to place this as an answer to your question. – diogoslima Feb 25 '20 at 20:58
  • Thank you @diogoslima, yes! you actually helped me to clarify things – Elin Feb 25 '20 at 21:51
  • I am happy I was able to help! I just summarized our chat in the answer below. Happy coding and good luck on your project. – diogoslima Feb 25 '20 at 22:40

4 Answers4

3

UML models can coexist for different purpose. For example, you could use an analysis diagram to understand the domain, a design diagram to show the conceptual solution, and an implementation diagram for the details of the real solution.

You can also keep a single model that evolves. But in this case, you'd loose the initial design to keep only the implementation model. Unfortunately this model is the least useful since it is somewhat redundant with the information in the code and is quickly obsolete.

I'd therefore strongly advise to keep both:

  1. the true design model that shows the classes as you see them;
  2. a database model with the tables to document the ORM mapping. For this you coud use a database profile for UML to add a «table» stereotype

In pure DB modelling it is also common to distinguish the logical model (entities, relationships) and the physical model (the tables implementing the logical model) for similar reasons.

Examples:

Christophe
  • 68,716
  • 7
  • 72
  • 138
2

The development of an app is supported by an entire set of (evolving) models, as illustrated in the diagram below.

The three main purposes of making UML class models when developing an app are:

  1. Describing the entity types of the app's problem domain for analyzing and better understanding the requirements for the app in a conceptual (domain) model.
  2. Designing the schema of the app's underlying database (this is typically an RDB schema defined with a bunch of CREATE TABLE statements).
  3. Designing the model classes of the data model of your app, which will be coded, e.g., as Java Entity classes or C# classes with EF annotations.

For 1 and 2, you may take a look at my book An introduction to information modeling and databases, while for 3 you may check out a book on model-based development, e.g. for Java Backend Apps or JavaScript Frontend Apps.

enter image description here

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • Please explain that this is just your opinion, not based on any internationally accepted standard. The Rational Unified Process (not very popular anymore) defines other models, see http://admiraalit.nl/admiraal/WhichUMLmodels.pdf – www.admiraalit.nl Feb 27 '20 at 11:13
  • 1
    @www.admiraalit.nl: My explanations are based on OMG's MDA standard. The RUP is not an open standard, but has been proposed by a company. – Gerd Wagner Feb 27 '20 at 11:44
1

In a nutshell (and as we discussed in the comments), your model is valid and fine :)

Of course, my answer is considering the decision was made with knowledge of both the application and the database requirements. And also understanding you have a good grasp of available options (such as the ones enumerated in this other post).

Usually, best practices guide us that a relational database and an application have independent designs, as they are built on top of different requirements. Therefore, there is no need to try perfectly matching their components, but make sure that data schema and relations are well defined.

diogoslima
  • 169
  • 7
1

Although the existing answers already describe the problem domain I want to add that independent class design and database models (both derived from an abstract analysis model) will evolve over time. Some tools support model translation and tracebility, but I would not trust these too far. Better to have your individual traceability and good mechanisms set up to keep the models in synch. That can get tricky over time and needs thorough analysis on top of any modeling.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86