Search code examples
javadatabasehibernatehibernate-mappingrdbms

How exactly works this Hibernate Many To Many relation implementation? Is it my reasoning correct?


I am pretty new in Hibernate and I have some doubt related this tutorial example that implement a ManyToMany use case.

So I have these 2 entity classes:

1) Movie:

@Entity
public class Movie {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToMany(cascade={CascadeType.PERSIST})
    @JoinTable(
                    name="movie_actor",
                    joinColumns={@JoinColumn(name="movie_id")},
                    inverseJoinColumns={@JoinColumn(name="actor_id")}
        )

    private Set<Actor> actors = new HashSet<Actor>();   

    public Movie() {}
    public Movie(String name) {
        this.name = name;
    }

    public Set<Actor> getActors() {
        return actors;
    }

}

2) Actor:

@Entity
public class Actor {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToMany(mappedBy="actors")
    private Set<Movie> movies = new HashSet<Movie>();

    public Actor() {}   
    public Actor(String name) {
        this.name = name;
    }

    public Set<Movie> getMovies() {
        return movies;
    }
}

So it mean that a Movie instance can be associated to many actors (many actors act into a single movie) and at the same time an Actor instance can be associated to many movie (a single actor can act in many movie).

So the Movie class is the owner of the many to many relation because it contains:

@ManyToMany(cascade={CascadeType.PERSIST})
@JoinTable(
                name="movie_actor",
                joinColumns={@JoinColumn(name="movie_id")},
                inverseJoinColumns={@JoinColumn(name="actor_id")}
    )

private Set<Actor> actors = new HashSet<Actor>();   

and the Actor class is the inverse end of the relation.

It means that, into the database, will be created a movie_actor association table that use the id of the Movie table and Actor table to create the relaion.

And it means that an Actor instance is not responsible about the update so if I add a new actor and I set a movie to it, the actor will be inserted into the Actor table but the relation is not insert into the movie_actor association table.

To do it I have to create a new Movie object, then set the actors on it and persist this Movie object, so in this way will be performed the Movie object on the Movie table, the related Actor objects on the Actor table and the related record into the movie_actor association table.

Is it my reasoning correct or am I missing something?

Tnx


Solution

  • "So the Movie class is the owner of the many to many relation because it contains..."

    No, the Movie is the owner because the Actor contains mappedBy:

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

    "To do it I have to create a new Movie object, then set the actors on it and persist this Movie object..."

    Yes, but it doesn't have to be a new Movie, it can be an existing one to which you can add existing or new Actor instances.

    Regarding everything else, the answer is: Yes, your reasoning is correct.