Frage

I have a relational model where entities usually have relationships with two qualifiers, start_date and end_date, so to keep a history of past relationships.

When presented with the task of creating a data model with Hibernate, I faced the problem of dealing with these relationships that break the usual many-to-many relationship case. For instance let's say a user can have a certain access permission (which is in fact a real case), and we want to model the history of permissions a user has had:

| USER   |      | has_permission |      | PERMISSION |
|--------|      |----------------|      |------------|
| userId |------|userId          |------|permissionId| 
| user   |      |permissionId    |      |permission  |
----------      |start_date      |      -------------
                |end_date        |
                ------------------

Now how would you do this in a class model? What one would do in a regular case, creating a User class with a list of Permissions is no longer valid. Given that I have lots of entities, I don't want to end up with lots of "HasX" classes that only hold up another reference plus the startDate and the endDate.

Whats the common way to solve this problem?

I use Hibernate, but I guess the answer would be language-agnostic.

War es hilfreich?

Lösung

Whenever you want to maintain the history of a many-to-many association (or relationship type), such as hasPermission, you have to turn it into a class, such as PermissionByUser, with attributes for the time interval that defines the valid-time of a specific link (or relationship), such as MichelReap-hasPermission-DeleteUser. So, you get a model like the following:

enter image description here

Replacing a many-to-many association with a class and two many-to-one associations is a well-known pattern (see, for instance, chapter 5 of my tutorial) . Notice that the "dots" at the User side and at the Permission side denote corresponding reference properties (userand permission) to be added to the PermissionByUser class.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top