I am having some trouble understanding the difference between @OneToMany
and @ManyToMany
. When I use @OneToMany
it defaults to create a JoinTable and if you add the mappedBy attribute you will have bidirectional relationship between two entities.
I have a Question
that may belong to many Categories
, and one Category
may belong to many Questions
. I don't understand if I should use @ManyToMany
or @OneToMany
because for me it seems exactly the same thing, but it is probably not.
Could somebody explain it?
In a One-To-Many relationship, one object is the "parent" and one is the "child". The parent controls the existence of the child. In a Many-To-Many, the existence of either type is dependent on something outside the both of them (in the larger application context).
In a database management system, a Many-to-One relationship is defined as a relationship between several instances of one entity and one instance of another entity. For example, it is possible for more than one student to work on a project.
Simply put, one-to-many mapping means that one row in a table is mapped to multiple rows in another table. Let's look at the following entity-relationship diagram to see a one-to-many association: For this example, we'll implement a cart system where we have a table for each cart and another table for each item.
All @JoinColumn does is to specify a column for joining an entity association or element collection. Since you have made @JoinColumn associated with Patient class object, that's why foreign key is created on Patient table.
Well, the difference is in the design you're trying to reflect using objects.
In your case, every Question
can be assigned to multiple Categories
- so that's a sign of @*ToMany
relationship. Now you have to decide if:
Category
can have only one Question
assigned to it (it will result in a unique constraint which means that no other Category can refer the same Question) - this will be @OneToMany
relationship,Category
can have multiple Questions
assigned to it (there will be no unique constraint in the Category
table) - this will be @ManyToMany
relationship.@OneToMany (Question -> Category)
This relationship can be represented by join table only if you explicitly define so using @JoinTable
or when it is a unidirectional relationship in which the owning side is the 'One' side (it means that in the Question
entity you have a collection of Categories
, but in the Categories
you don't have any reference to the Question
).
If you think about it, it seems quite reasonable that the join table is used. There is no other way the DBMS could save a connection between one row in Question
table with multiple rows in Categories
table.
However, if you would like to model a bidirectional relationship you need to specify that the Category
('Many' side) is the owning side of the relationship. In this case the DBMS can create a join column with foreign key in the Category
table because each Category
row can be connected with only one Question
.
In this way you don't have any join table but simple foreign keys (still, as pointed at the beginning, you can force to create the join table using @JoinTable
).
@ManyToMany
This relationship must be represented as a join table. It basically works very similar to the unidirectional @OneToMany
relationship, but in this case you may have multiple rows from Question
joined with multiple rows from Categories
.
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