dirai dirai - 3 months ago 34
JSON Question

Losing the child data in a bi-directional relationship when using jacksonMapper

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?

Answer

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!

Update on 15 Aug 2016 Independence Day:

@JsonBackReference <-> @JsonIgnore Hence the child is omitted when the parent is serialized

JsonManagedReference vs JsonBackReference

Comments