我有一个实体电影,然后是人物实体。 电影与Person有一个ManyToMany关系:一个是铸造,另一个是生产,另一个是直接。
所以我的问题是给了一个人如何获得他所采取的行动,制作和指导的电影。
//人员类:
@Entity
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@JsonView(Views.Private.class)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPerson;
private String name;
private String surnames;
private boolean isActor;
private boolean isDirector;
private boolean isProducer;
@ManyToMany(mappedBy = "casting")
private Set<Movie> moviesCasting = new HashSet<>();
@ManyToMany(mappedBy = "movieDirection")
private Set<Movie> movieDirection = new HashSet<>();
@ManyToMany(mappedBy = "movieProduction")
private Set<Movie> movieProduction = new HashSet<>();
//电影课
@Entity
public class Movie implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "casting_movie",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "idPerson")
)
private Set<Person> casting = new HashSet<>();
public Set<Person> getCasting() {
casting.size();
return casting;
}
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "production_movie",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "idPerson")
)
private Set<Person> movieProduction = new HashSet<>();
public Set<Person> getMovieProduction() {
movieProduction.size();
return movieProduction;
}
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "direction_movie",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "idPerson")
)
private Set<Person> movieDirection = new HashSet<>();
getters and setters(...)
当我想获取有关某人的信息时,我收到错误:未能懒惰地初始化角色集合Movies.casting“
如果我给了一个人,我想获得每个“电影摄影”,我必须双向创建ManyToMany关系吗?
答案 0 :(得分:0)
您可以拥有另一个实体,该实体将具有多个对Movie和Person的引用,并具有一个额外的字段RelationshipType(具有值Casting,Production,Direction等)。你应该为它创建一个枚举。
通过这样做,你可以获取一个人的定向电影(来自新实体):
findByMovieAndPersonAndRelationshipType(Movie movie, Person person, RelationshipType.DIRECTED);
P.S。摆脱你所有的多对多关系