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


i pretty new in hibernate , have doubt related tutorial example implement manytomany use case.

so 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 mean movie instance can associated many actors (many actors act single movie) , @ same time actor instance can associated many movie (a single actor can act in many movie).

so movie class owner of many many relation because 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 actor class inverse end of relation.

it means that, database, created movie_actor association table use id of movie table , actor table create relaion.

and means actor instance not responsible update if add new actor , set movie it, actor inserted actor table relation not insert movie_actor association table.

to have create new movie object, set actors on , persist movie object, in way performed movie object on movie table, related actor objects on actor table , related record movie_actor association table.

is reasoning correct or missing something?

tnx

"so movie class owner of many many relation because contains..."

no, movie owner because actor contains mappedby:

the field owns relationship. required unless relationship unidirectional.

"to have create new movie object, set actors on , persist movie object..."

yes, doesn't have new movie, can existing 1 can add existing or new actor instances.

regarding else, answer is: yes, reasoning correct.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -