I wonder what the best practice is to deal with a data model like this:
We have 3 entities:
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.
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.
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.
A bidirectional relationship is valid in both directions. Intersects is an example of a bidirectional relationship.
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.
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
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With