Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Saturday

JPA-(Java Persistence API)- Tutorial ManyToMany unidirectional and bidirectional

Tutorial ManyToMany unidirectional and bidirectional

In a ManyToMany example a person may have several dogs and a dog may have several persons (imagine a dog
that lives in a house with 15 persons).

In a ManyToMany approach it is necessary to use an extra database table to store the ids that relate the database tables of every entity of the relationship. Thus for the specific example we will have a person table, a dog table and a relationship table named person_dog. The person_dog table would only maintain the person_id and dog_id values that represent which dog belongs to which person.

Check the Person entity below:

import java.util.List;
import javax.persistence.*;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
@ManyToMany
@JoinTable(name = "person_dog", joinColumns = @JoinColumn(name = "person_id"), inverseJoinColumns =
@JoinColumn(name = "dog_id"))
private List<Dog> dogs;
@OneToOne
@JoinColumn(name = "cellular_id")
private Cellular cellular;
// get and set
}

About the code above:
•The @ManyToMany annotation is used.
•The @JoinTable annotation is used to set the relationship table between the entities; “name”
sets the table name; “joinColumn” defines the column name in the table of the relationship
owner; “inverseJoinColumns” defines the column name in the table of the non-relationship
owner.
The Person entity has unidirectional relationship with the Dog entity. Check how the Dog entity would look like in a
bidirectional relationship:

import java.util.List;
import javax.persistence.*;
@Entity
public class Dog {
@Id
@GeneratedValue
private int id;
private String name;
@ManyToMany(mappedBy="dogs")
private List<Person> persons;
// get and set
}

As you can see the @ManyToMany annotation is configured with the "mappedBy" option, which establishes the Person entity as the relationship owner.
Every relationship needs that one of the entities be the “relationship owner”. For the ManyToMany association the relationship owner entity is the one that is dictated by the "mappedBy" option usually copnfigured with the @ManyToMany annotation at the non-owner entity of the relation. If the "mappedBy" option is not found in any of
the related entities JPA will define both entities as the relationship owners. The "mappedBy" option must point to the associated entity's attribute name and not the associated entity's name.
There is no such “auto relationship”
For a Bidirectional relationship to work correctly it is necessary to do like below:

person.setDog(dogs);
dog.setPersons(persons);

JPA uses the Java concept of class reference, a class must maintain a reference to another one if there will be a join between them. JPA will not create a relationship automatically; to have the relationship in both sides it is needed to do like above.

NEXT

No comments:

Post a Comment