Hibernate @OneToMany关系导致JSON结果无限循环或空条目 [英] Hibernate @OneToMany Relationship Causes Infinite Loop Or Empty Entries in JSON Result

查看:359
本文介绍了Hibernate @OneToMany关系导致JSON结果无限循环或空条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,一个实体电影"和一个实体剪辑" 每个剪辑都属于一部电影,一部电影可以有多个剪辑.

I have two entities, an entity "movie" and an entity "Clip" each clip belongs to one movie and a movie can have multiple clips.

我的代码如下:

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;

正在生成表,并且每个Clip都有一列"movie_id",但这会导致我的应用程序在请求数据时陷入无限循环

The tables are being generated and each Clip has a column "movie_id" but this causes my application to end up in an infinite loop when I'm requesting Data

    @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...

当我请求剪辑时,结果相同.

It's the same result when I'm requesting a clip.

当我将其更改为@ManyToMany关系时,不会有类似的问题,但这不是我所需要的.你能帮助我吗?将fetchType设置为Lazy无效.

When I change it to a @ManyToMany relationship there won't be any problems like that, but that's not what I need here. Can you help me? Setting fetchType to Lazy didn't work.

我正在使用当前的JBoss开发工作室

I'm using the current JBoss development studio

我通过阅读这篇文章解决了"这个问题:

I "solved" this, by reading this article:

http://blog.jonasbandi.net/2009/02/help -needed-mapping-bidirectional-list.html

要以一对多方为拥有方映射双向一对多,必须删除mapledBy元素,并将多对一设置为@JoinColumn,以将其可插入,并将其更新为false.此解决方案是显然没有进行优化,并且会产生一些其他的UPDATE语句."

"To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is obviously not optimized and will produce some additional UPDATE statements."

当我请求电影时,我得到以下答案:

when I request a movie I get the following answer:

{"id":1,版本":1,名称":"MGS演练",文件名":"video.mp4",电影类别":[{"id":1,版本" :1,名称":演练"}],片段":[],描述":预告片zu mgs4"}

{"id":1,"version":1,"name":"MGS Walkthrough","filename":"video.mp4","movieCategories":[{"id":1,"version":1,"name":"Walkthrough"}],"clips":[],"description":"Trailer zu mgs4"}

条目剪辑"仍然出现.这仍然是错误的解决方案,还是我必须忍受这个?

the entry "clips" still appears. Is this still the wrong solution or do I just have to live with this?

推荐答案

我遇到了完全相同的问题.我尝试了引用的段落中的解决方案,但该解决方案对我不起作用.

I ran into exactly the same problem. I tried the solution from the quoted paragraph, it did not work for me.

我所做的是在Clip类中为getMovie()返回null,然后无限循环问题消失了.以JSON格式返回的数据将类似于{"movieId":1 ... clips:["clipId":1,"movie":"null",..]}.

What I did is to return null for getMovie() in Clip class, then the infinite loop issue is gone. The data returned in JSON format will look like {"movieId":1 ... clips:["clipId":1, "movie":"null", ..]}.

如果您还想进一步删除JSON中的movie属性,则将类级别的注释添加到Clip类中 @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)

If you also want further remove the movie property in JSON, add the class-level annotation to Clip class @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

杰克逊功能:防止对空值和默认值进行序列化

更新: 我发现更简单的方法是在Clip类中删除电影的吸气剂.

Update: The easier way I found is to simply remove the getter of movie in Clip class.

这篇关于Hibernate @OneToMany关系导致JSON结果无限循环或空条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆