1

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?

raikumardipak
  • 1,461
  • 2
  • 29
  • 49
  • Could you add your db query too ? – k1133 Apr 25 '16 at 02:56
  • @k1133 the db side I m using hibernate and it's a select all query on FoodItem. I want the categories to also come with foodItem. But in using jacksonMapper the categories are left out in the json string returned. – raikumardipak Apr 25 '16 at 03:00
  • I have 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. But it still does ignore them! :( – raikumardipak Apr 25 '16 at 05:52
  • Issue could be because of infite loop while trying to map parent/child. As parent has childSet ad child a parentSet . It would essentially go into infinite loop while de-serializing . could you put a break point and confirm that you query result has no childSet ? – k1133 Apr 25 '16 at 06:00
  • @k1133 There is no infinite loop as the Child class still has JsonIgnore . It's only the Parent class from which JsonIgnore is removed so that jacksonMapper doesn't ignore it while writing it into a string. I am having a look in this question too http://stackoverflow.com/questions/10065002/jackson-serialization-of-entities-with-birectional-relationships-avoiding-cyc – raikumardipak Apr 25 '16 at 06:02

1 Answers1

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

raikumardipak
  • 1,461
  • 2
  • 29
  • 49