Jackson JSON User Group

JSON in Java the Right Way -- Action, Jackson!

Using "bi-directional" feature (solving parent/child, one-to-many in ORM)

As you may or may not know, one of cool new features in Jackson 1.6 was the way to solve problem of parent-child links: cases where you have "back links" from one object to another. These are particularly common in ORM situations.

While feature itself has documentation here, there's a good article, "Jackson to rescue", which I think you might find useful.

In general, it would be great to post links to useful articles here; I will also try to update FasterXML Jackson Wiki with links where appropriate.

Views: 5113

Tags: ORM, jackson, java, json, one-to-many

Comment by reda on April 10, 2012 at 8:36am

Hi,

I have difficulties understanding @JsonManagedReference and @JsonBackReference

From the javadoc:

@JsonBackReference:

Annotation used to indicate that associated property is part of two-way linkage between fields; and that its role is "child" (or "back") link. Value type of the property must be a bean: it can not be a Collection, Map, Array or enumeration.

@JsonManagedReference

Annotation used to indicate that annotated property is part of two-way linkage between fields; and that its role is "parent" (or "forward") link. Value type (class) of property must have a single compatible property annotated with JsonBackReference.

Which means that in the relationship of customer/orders where the customer is the parent and order is the child we should have some thing like:

Class customer {

  @JsonManagedReference //Isn't order the child? How can the javadoc states that its role is "parent"

  List<Order> order ;

}

Class Order {

  @JsonBackReference //Isn't the customer the parent? How can the javadoc states that its role is "child"

  Customer customer;

}

Please clarify this for me.

Comment by Tatu Saloranta on April 10, 2012 at 9:48am

Javadoc looks bit confusing; but what it tries to say is that _property_ that points to child(ren) is the "parent link" (from parent to child). Term "forward link" is probably better. Comment is not to mean that what is being pointed to was the parent entity, on contrary.

Hope this helps!

Comment by reda on April 10, 2012 at 10:21am

Thanks for your quick reply.

++

Comment

You need to be a member of Jackson JSON User Group to add comments!

Join Jackson JSON User Group

© 2014   Created by Tatu Saloranta.   Powered by

Badges  |  Report an Issue  |  Terms of Service