Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate @OneToMany Relationship Causes Infinite Loop Or Empty Entries in JSON Result
    primarykey
    data
    text
    <p>I have two entities, an entity "movie" and an entity "Clip" each clip belongs to one movie and a movie can have multiple clips.</p> <p>My code looks like:</p> <pre><code>Movie.java @OneToMany(mappedBy = "movie", targetEntity = Clip.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) private Set&lt;Clip&gt; clips = new HashSet&lt;Clip&gt;(); Clip.java @ManyToOne @JoinColumn(name="movie_id") private Movie movie; </code></pre> <p>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</p> <pre><code> @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... </code></pre> <p>It's the same result when I'm requesting a clip.</p> <p>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.</p> <p>Edit: I'm using the current JBoss development studio</p> <p>Edit: </p> <p>I "solved" this, by reading this article:</p> <p><a href="http://blog.jonasbandi.net/2009/02/help-needed-mapping-bidirectional-list.html">http://blog.jonasbandi.net/2009/02/help-needed-mapping-bidirectional-list.html</a></p> <p>"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."</p> <p>when I request a movie I get the following answer:</p> <p>{"id":1,"version":1,"name":"MGS Walkthrough","filename":"video.mp4","movieCategories":[{"id":1,"version":1,"name":"Walkthrough"}],"clips":[],"description":"Trailer zu mgs4"}</p> <p>the entry "clips" still appears. Is this still the wrong solution or do I just have to live with this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload