Losing the child data in a bi-directional relation

2019-06-13 17:54发布

问题:

I have two classes as shown below in a bi-directional Many to Many relationship:

  Parent implements Serializable{

        @ManytoMany(//declaration for join table)
        @JsonBackReference
        @com.fasterxml.jackson.annotation.JsonIgnore
        Set <Child> childSet;

    }
    Child implements Serializable{
    @ManytoMany(//declaration for join table)
    @JsonManagedReference
    @com.fasterxml.jackson.annotation.JsonIgnore
    Set <Parent> parentSet;
    // other getter and setters
    }

I make a call in my DAO to get a particular parent. Alongwith the parent details I want to fetch the children of the parent. Something like this:

   Hibernate.initialize(parent.getChildSet()); //this works perfectly
// and I get the details of parent along with the children in my DAO call.

But when I do the below in my business service while returning the data to the controller the children are omitted from the parent json string.

jacksonMapper.writeValueAsString(parent); 

So i removed the @JsonIgnore on Child attribute inside Parent class thinking that jackson might understand that these fields are not to be ignored while writing to a string as shown below. But it still does ignore them! :(

 Parent implements Serializable{

    @ManytoMany(//declaration for join table)
    @JsonBackReference
    //@com.fasterxml.jackson.annotation.JsonIgnore
    Set <Child> childSet;

}

Any idea where I might be going wrong?

回答1:

I have not been able to find out why this is happening. Meanwhile I have opted for a workaround. I am making two separate calls to DB. One to fetch the parent first and then second to fetch the child based on the fetched parentId.

Alternatively, I can make both the DB calls at the service same time and prepare the JSON as a complex string before sending it to the ui:

complex:{
parent:parent,
child:child
}

In either case, it is a workaround. The ideal solution is just remove@JsonIgnore in the mapping only from the parent side for the child class. But somehow that doesn't seem to work. I'll post in case I find why the "ideal" solution is not working!

Ideal Solution Updated as answer on 15 Aug 2016 The Independence Day of India:

The problem is in the mapping:

Parent implements Serializable{

        @ManytoMany(//declaration for join table)
        @JsonBackReference
        @com.fasterxml.jackson.annotation.JsonIgnore
        Set <Child> childSet;

    }

When you say @JsonBackReference it actually means ignore this field while writing the Json, i.e. to say,

@JsonBackReference <-> @JsonIgnore

Hence the child is omitted when the parent is serialized. With ORM mappings it's always a best practice to have the annotations one sided rather than double sided. In that way, you can avoid a lot of unwanted exceptions while fetching the data and secondly, keep your business code clean.

JsonManagedReference vs JsonBackReference