【发布时间】: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