Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice: Should one avoid bi-directional relationships? [closed]

I wonder what the best practice is to deal with a data model like this: Simple security model

We have 3 entities:

  • Role
  • User
  • Permission

Note that the entities are represented as java classes and will be mapped to a database via hibernate, anyway I think the question could be answered without having knowledge of these technologies.

There is a many-to-many relationship between Role & User and between Role & Permission.

Is it ok to have a bi-directional relationship here? So, that you can ask Role to give you all his members and to ask User to give you all his roles.

It's very comfortable that you can ask both entities, however one drawback is that whenever you remove a relationship you have to manage both entities.

E.g. if you remove Role from a User you have also to remove the User from the Role. This can be quite annoying if there are many of these relationships. Therefore I would like what the best practice is.

like image 418
flash Avatar asked Dec 21 '11 12:12

flash


People also ask

How do you avoid the bidirectional relationship in power bi?

There's a better way to achieve the same result: Instead of using bi-directional filters, you can apply a visual-level filter to the Product slicer itself. Let's now consider that the relationship between the Product and Sales table no longer filters in both directions.

What is a bidirectional relationship?

In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class's code can access its related object. If an entity has a related field, the entity is said to “know” about its related object.

Which is an example of a bidirectional relationship?

A bidirectional relationship is valid in both directions. Intersects is an example of a bidirectional relationship.

What is a bidirectional relationship in power bi?

Bidirectional cross-filtering enables them to apply filters on both sides of a table relationship. You can apply the filters by propagating the filter context to a second related table on the other side of a table relationship.


2 Answers

I try to avoid bidirectional relationships. Instead replace one direction with an explicit query in you DAO/Repository. Keeps the model simpler and if done correctly via interfaces the application clean of circular dependencies

like image 195
Jens Schauder Avatar answered Sep 21 '22 21:09

Jens Schauder


Is it ok to have a bi-directional relationship here? So, that you can ask Role to give you all his members and to ask User to give you all his roles.

Yes. If you actually need the navigability in both ways, then there's no reason to prevent yourself from obtaining it in the simplest way possible.

one drawback is that whenever you remove a relationship you have to manage both entities.

This depends on the implementation, one could implement the data model in a way that would synchronise both "ends".

like image 25
Kos Avatar answered Sep 20 '22 21:09

Kos