LuckyLuke
LuckyLuke

Reputation: 49097

Difference between using @OneToMany and @ManyToMany

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?

Upvotes: 16

Views: 13605

Answers (3)

Mr.J4mes
Mr.J4mes

Reputation: 9266

In your Questions & Categories case, you should use @ManyToMany relationship. @ManyToMany basically means that "a Question can belong to many Categories at the same time" and "a Category can contain many Questions at the same time". A new table will automatically be created to store the mappings. Your code would look like this:

@Entity
public class Question implements Serializable {
    ...
    @ManyToMany
    private List<Category> categories;
    ...
}

@Entity
public class Category implements Serializable {
    ...
    @ManyToMany
    private List<Question> questions;
    ...
}

If you use @OneToMany relationship for your Questions and Categories (let's say Category on the One side and Questions on the other), this means that "a Question can only belong to one Category" and "a Category can contain many Questions at the same time". No new table is needed to store the mapping. Instead, a new field will automatically be created in the Many side to record the ID of the One side. Your code would look like this:

@Entity
public class Question implements Serializable {
    ...
    @ManyToOne
    private Category theCategory;
    ...
}

@Entity
public class Category implements Serializable {
    ...
    @OneToMany(mappedBy="theCategory")
    private List<Question> questions;
    ...
}

Upvotes: 1

Piotr Nowicki
Piotr Nowicki

Reputation: 18224

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:

  • each 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,
  • each 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.

Upvotes: 33

Jonathan Schneider
Jonathan Schneider

Reputation: 27757

@ManyToMany relationships have mutually referential foreign keys on both sides of the relationship. Sometimes, this relationship is mediated by an adjoining table.

@OneToMany relationships have a foreign key on the "one" side and not on the "many" side. In a @OneToMany relationship, one object is the "parent" and one is the "child". The parent controls the existence of the child.

Remember that a @ManyToMany bi-directional relationship need not be symmetric!

Upvotes: 1

Related Questions