【问题标题】:Many To Many Relation JPA多对多关系 JPA
【发布时间】:2018-04-15 11:41:04
【问题描述】:

我想在实体 Movie 和 Genre 之间创建多对多关系。因为一部电影可以有很多类型,而一个类型可以有很多电影。但我不知道如何进行关联。我必须创建实体“genre_movie”?如果我编译它,生成更多表:movie_genre 和genre_movie。我只想要一个。

@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;
}

体裁类

 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;
     }
}

还有查询:

 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');

【问题讨论】:

  • 但是我不知道怎么关联:那么,你有没有阅读文档来了解一下?似乎没有。为什么哦为什么?在这种情况下,这不是最合乎逻辑的事情吗? docs.jboss.org/hibernate/orm/current/userguide/html_single/…
  • 当您检查文档时,您会发现使用 mappedBy 将关系设置为 BIDIRECTIONAL,您没有使用过。
  • 我已经阅读了该文档,因为我收到错误“无法延迟初始化角色集合:org.udg.pds.simpleapp_javaee.model.Movie.genres,无法初始化?

标签: java jpa persistence


【解决方案1】:
@Entity
public class Movie implements Serializable {
    @ManyToMany
    private Set<Genre> teacherSet;
}

@Entity
public class Genre implements Serializable  {
     @ManyToMany(mappedBy="teacherSet")
     private Set<Movie> movieSet;

}

在此示例中,将仅在 Movie 和 Genre 之间生成一个关系,其中关系的所有者将是 Movie。在 SQL 中,会生成一个关联表 Movie_Genre。

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2011-12-29
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多