我有两个实体,一个叫做“电影”,另一个叫做“剪辑”。每个剪辑属于一个电影,一个电影可以拥有多个剪辑。
我的代码如下:
Movie.java
@OneToMany(mappedBy = "movie", targetEntity = Clip.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Clip> clips = new HashSet<Clip>();
Clip.java
@ManyToOne
@JoinColumn(name="movie_id")
private Movie movie;
正在生成,每个都有一个“movie_id”列,但这会导致我请求数据时陷入无限循环 @Path("/{id:[0-9][0-9]*}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Movie lookupMovieById(@PathParam("id") long id) {
return em.find(Movie.class, id);
}
result:
{"id":1,"version":1,"name":"MGS Walkthrough","filename":"video.mp4","movieCategories":[{"id":1,"version":1,"name":"Walkthrough"}],"clips":[{"id":1,"version":1,"name":"MGS Walkthrough P1","keywords":null,"movie":{"id":1,"version":1,"name":"MGS Walkthrough","filename":"video.mp4","movieCategories":[{"id":1,"version":1,"name":"Walkthrough"}],"clips":[{"id":1,"version":1,"name":"MGS Walkthrough P1","keywords":null,"movie":{"id":1,"version":1,"name":"MGS Walkthrough","filename":"video.mp4","movieCategories":[{"id":1,"version":1,"name":"Walkthrough"}],"clips":[{"id":1,"version":1,"name":"M...
当我请求一个剪辑时,得到的结果与请求一个电影时相同。
当我将其改为@ManyToMany关系时,就不会出现这样的问题,但这不是我此处所需的。你能帮我吗?将fetchType设置为Lazy并没有起作用。
编辑:我正在使用当前的JBoss开发工具
编辑:
我通过阅读这篇文章来“解决”这个问题:
“要映射双向一对多,其中一对多面作为所有者端,必须删除mappedBy元素,并将多对一@JoinColumn设置为不可插入和不可更新。这个解决方案显然并不优化,并将产生一些额外的UPDATE语句。”
当我请求一个电影时,得到以下答复:
{"id":1,"version":1,"name":"MGS Walkthrough","filename":"video.mp4","movieCategories":[{"id":1,"version":1,"name":"Walkthrough"}],"clips":[],"description":"Trailer zu mgs4"}
“clips”条目仍然出现。这仍然是错误的解决方案,还是我只能接受这个结果?