There are 4 types of entity relationships in MikroORM:
Relations can be unidirectional and bidirectional. Unidirectional are defined only on one
side (the owning side). Bidirectional are defined on both sides, while one is owning side
(where references are store), marked by
inversedBy attribute pointing to the inverse side.
On the inversed side we define it with
mappedBy attribute pointing back to the owner:
When modeling bidirectional relationship, you can also omit the
mappedByon the inverse side is enough as it will be auto-wired.
Many instances of the current Entity refer to One instance of the referred Entity.
There are multiple ways how to define the relationship, all of following is equivalent:
You can also specify how operations on given entity should cascade to the referred entity.
One instance of the current Entity has Many instances (references) to the referred Entity.
Again, all of following is equivalent:
As you can see, OneToMany is the inverse side of ManyToOne (which is the owning side). More about how collections work can be found on collections page.
One instance of the current Entity refers to One instance of the referred Entity.
This is a variant of ManyToOne, where there is always just one entity on both sides. This means that the foreign key column is also unique.
As you can see, relationships can be also self-referencing (all of them. OneToOne also supports Orphan Removal.
Many instances of the current Entity refers to Many instances of the referred Entity.
Here are examples of how you can define ManyToMany relationship:
Again, more information about how collections work can be found on collections page.