Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Bidirectional @OneToMany - infinitely nested JSON objects
PostPosted: Wed Apr 12, 2017 1:53 pm 
Newbie

Joined: Wed Apr 05, 2017 2:10 am
Posts: 4
Update: I've managed to fix some minor errors and at this stage, the parent object (Entry) gets saved, but the child (Address) does not.
-----------------------------------------------

Hello,

I've implemented bidirectional @OneToMany mapping. When done, I'm sending the parent object to the RESTful api.
So the entities are on the API side. On client side I'm using DTO objects that of course very much resemble the entity API classes.

I had to annotate as in the following code snippet the DTOs to avoid an exception saying something about inifinite recursion.

Code:
public class EntryDto {

   private Long id;
   
   @NotNull
   private String name;

        @JsonManagedReference
   private List<AddressDto> addresses = new ArrayList<>();

    .....


public class AddressDto {

    private Long id;

    @NotNull
    private String line1;

    @Null
    private String line2;

    @NotNull
    @JsonBackReference
    private EntryDto entryDto;

    public AddressDto(String line1, String line2){
        this.line1 = line1;
        this.line2 = line2;
    }

    public AddressDto(String line1, EntryDto entryId){
        this.line1 = line1;
        this.entryDto = entryId;
    }



The exception is gone when I annotated like above.

However, when I debug my API app, I notice that the objects are still inifinitely nested.

When I set the breakpoing on the line where I'm saving object Entry entry, I can see that:

entry
id = null
name = "name i gave it"
addressess
0 // the single assigned address
id = null
line1 = "value I set for line 1"
line2 = "value I set for line 2"
entry
entry
id = null
name = "name i gave it"
addressess
0 // the single assigned address
id = null
line1 = "value I set for line 1"
line2 = "value I set for line 2"
entry
... and so on


How do I deal with this issue?

------------------------------------------------
Update: I've managed to fix some minor errors and at this stage, the parent object (Entry) gets saved, but the child (Address) does not.


Last edited by java9 on Wed Apr 12, 2017 5:35 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Bidirect. @OneToMany - issue with infinitely nested objects
PostPosted: Wed Apr 12, 2017 5:35 pm 
Newbie

Joined: Wed Apr 05, 2017 2:10 am
Posts: 4
@Vlad's reply on this thread: https://forum.hibernate.org/viewtopic.php?f=1&t=1044177 helped me to resolve my problem.

I started out with cascade = CascadeType.ALL but eventually removed it in order to narrow the possible causes of other (minor) errors I was getting in the console.

Vlad says that the actual saving of the "child" object depends on this "cascade" thing. I learned a valuable lesson from this and I'm sure someone else will face the same issue so I'm posting this here.


Top
 Profile  
 
 Post subject: Re: Bidirect. @OneToMany - issue with infinitely nested objects
PostPosted: Thu Apr 13, 2017 12:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1105
Try using @JsonBackReference for the one-to-many

Code:
@JsonBackReference
private List<AddressDto> addresses = new ArrayList<>();


and @JsonManagedReference for the many-to-one:

Code:
@JsonManagedReference
private EntryDto entryDto;


or, just set @JsonIgnore on the one-to-many side and leave only the many-to-one side to be marked in JSON.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.