Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MappedBy in bi-directional @ManyToMany : what is the reason

  1. What is the reason for setting MappedBy in bidirectional many-to-many relationships?
  2. When one table has significant amount of records, while other has a few, which side is better to put mappedBy?
like image 514
Dmitry Avatar asked May 15 '16 20:05

Dmitry


People also ask

What does mappedBy mean in Hibernate?

mappedBy tells Hibernate how to create instances of your entities and load the data into them. It should refer to the field name in the class that you are annotating, PersonDetail in this instance, where the relationship is defined.

What does bi directional relationship mean?

A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side. The owning side of a relationship determines how the Persistence runtime makes updates to the relationship in the database.

Who is the owner of a relationship in a bidirectional relationship?

The many side is always the owning side of the relationship. For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key. For many-to-many bidirectional relationships, either side may be the owning side.

What is the use of bidirectional mapping in Hibernate?

Bidirectional association allows us to fetch details of dependent object from both side. In such case, we have the reference of two classes in each other. Let's take an example of Employee and Address, if Employee class has-a reference of Address and Address has a reference of Employee.


2 Answers

It's actually a good question, and it helps to understand the concept of an "owning" entity. If you want to prevent both sides (in a bidirectional relationship) from having join tables, a good idea, then you need to have a mappedBy= element on one side.

Whether or not there is a join table is controlled by the mappedBy="name" element of the @ManyToMany annotation. The Javadoc for mappedBy for the ManyToMany annotation says:

The field that owns the relationship. Required unless the relationship is unidirectional.

For your (bidirectional) example, if there were only two @ManyToMany annotations and no mappedBy= element, the default will have two Entity tables and two Join Tables:

Hibernate: create table SideA (id bigint not null, primary key (id))
Hibernate: create table SideA_SideB (sidea_id bigint not null, sidebs_id bigint not null, primary key (sidea_id, sidebs_id))
Hibernate: create table SideB (id bigint not null, primary key (id))
Hibernate: create table SideB_SideA (sideb_id bigint not null, sideas_id bigint not null, primary key (sideb_id, sideas_id))

While this is saying that each Entity "owns" its ManyToMany relationship, the extra join table is redundant in the typical use case, and the Javadoc says you need a mappedBy annotation. If I decide to have SideA "own" the relationship, then I add the mappedBy= element to the SideB entity to specify that it doesn't own the relationship:

@Entity
public class SideA {
    @ManyToMany
    Set<SideB> sidebs;
}
@Entity
public class SideB {
    @ManyToMany(mappedBy="sidebs")
    Set<SideA> sideas;
}

Since the SideB entity no longer owns its ManyToMany relationship, the extra JoinTable will not be created:

Hibernate: create table SideA (id bigint not null, primary key (id))
Hibernate: create table SideB (id bigint not null, primary key (id))
Hibernate: create table SideA_SideB (sideas_id bigint not null, sidebs_id bigint not null, primary key (sideas_id, sidebs_id))

This is important to the developer because he or she must understand that no relationship is persisted unless it's added to the owning entity, in this case the SideA entity.

So, if you have a bidirectional ManyToMany relationship, which means you have ManyToMany on both entities involved, then you should add a mappedBy="name" on one of them as per the Javadoc and to avoid having a redundant join table.

As to which side to make the owning entity, there is no correct answer, it depends on what your system thinks is best. The relationship will only be persisted when entries are put in the owning side so you have to ask yourself whether you more commonly change a SideA's list or SideB's list. If SideA owns the relationship then you update the relationship by adding or removing SideB instances from a SideA instance but if you had a list of SideA instances for a SideB that you wanted to persist you would need to iterate through the list and alter each instance of SideA in the list.

As always, it's always a good idea to enable the sql logs and see what's going on in the database:

EDIT: If you have a persistence provider that only creates a single join table with no mappedBy setting then you have to check with the docs to see which side "owns" the relationship. Could be that neither or both sides own it and that updating neither or either side will persist the entity.

References:

What is the difference between Unidirectional and Bidirectional associations?.

What does relationship owner means in bidirectional relationship?.

What is the “owning side” in an ORM mapping?.

Most efficient way to prevent an infinite recursion in toString()?.

like image 66
K.Nicholas Avatar answered Oct 04 '22 00:10

K.Nicholas


mappedBy links both sides of a BIDIRECTIONAL relation. You put mappedBy on the OWNER of the relation, not based on how many records something has (aka object oriented design). You will find this information in any JPA tutorial and documentation.

like image 30
Neil Stockton Avatar answered Oct 03 '22 23:10

Neil Stockton