Home Many To Many Relation JPA
Reply: 0

Many To Many Relation JPA

user2751
1#
user2751 Published in April 25, 2018, 6:15 pm

I want to create a ManyToMany relation between the entites Movie and Genre. Because a Movie can have lots of genres, and a Genre can have lots of movies. But I don't know how to do the association. I have to create the entity "genre_movie"? If I compile it, generetes to more tables : movie_genre and genre_movie. I only want one.

@Entity
public class Movie implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @JsonView(Views.Private.class)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @JsonView(Views.Private.class)
    private String name;

    @JsonView(Views.Private.class)
    private Long year;
    @JsonView(Views.Private.class)
    private String sinopsis;
    @JsonView(Views.Private.class)
    private Double puntuation;
    @JsonView(Views.Private.class)
    private Long duration;
    @JsonView(Views.Private.class)
    private String idioma;
    @JsonView(Views.Private.class)
    private String trailer;

    //getters seters

    @ManyToMany(targetEntity=Genre.class)
    private Set teacherSet;
}

Genre Class

 import com.fasterxml.jackson.annotation.JsonView;

 import javax.persistence.*;
 import java.io.Serializable;
 import java.util.*;

 @Entity
 public class Genre implements Serializable  {

     private static final long serialVersionUID = 1L;

     @Id
     @JsonView(Views.Private.class)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;


     @JsonView(Views.Private.class)
     private String genreName;


     public Genre(){}

     public Genre(String genreName){
     this.genreName = genreName;
     }


     @ManyToMany(targetEntity = Movie.class)
     private Set movieSet;

     public String getName() {
     return genreName;
     }

     public void setName(String name) {
     this.genreName = name;
     }
}

And queries:

 insert into Movie (name,year,sinopsis,puntuation,duration,idioma,trailer) values ('Titanic',1997,'Is about a movie ....',0,88,'english','https://www.youtube.com/watch?v=zCy5WQ9S4c0&t=112s');
 insert into Movie (name,year,sinopsis,puntuation,duration,idioma,trailer) values ('Nightcrawler',2014,'Is about a movie dpsvr....',0,175,'english','https://www.youtube.com/watch?v=u1uP_8VJkDQ');
 insert into Movie (name,year,sinopsis,,,idioma,trailer) values ('Iron Man 2',2010,'Is about a movie dpsvr....',0,145,'english','https://www.youtube.com/watch?v=BoohRoVA9WQ');

 insert into Genre (genreName) values ('comedy');
 insert into Genre (genreName) values ('drama');
 insert into Genre (genreName) values ('thriller');
 insert into Genre (genreName) values ('terror');
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.320638 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO