8

I have read

looking for solutions to map One-To-One relationships. However, those demos in the articles are all Many-To-One or One-To-Many. From the demo, I conclude that in most cases, we will annotate the ONE side as @JsonManagedReference and the MANY side as @JsonBackReference.

However, there's no hint about how to map One-To-One. Do you have any idea?

Also, what if it is a bi-directional one-to-one?

Steinway Wu
  • 1,288
  • 1
  • 12
  • 18

1 Answers1

16

You can think of it as just a special case of one-to-many; so you will annotate reference from 'parent' to 'child' as @JsonManagedReference, and 'child' to 'parent' as @JsonBackReference.

One more document that might be helpful is the one at FasterXML Jackson Wiki; first example is for one-to-one relationship, and should be applicable for your case.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Thank you! Your answer is quite useful for me. By the way, what if I want to map a Bi-directional One To One? – Steinway Wu Jul 20 '11 at 14:32
  • Fully bi-directional case is cyclic, so it's not supported currently: it would need proper handling of cyclic graphs (which could then also handle all other combinations, but require metadata in JSON, object ids or such) – StaxMan Jul 20 '11 at 17:04
  • 1
    @JsonIdentityInfo will work. [JacksonFeatureObjectIdentity](http://wiki.fasterxml.com/JacksonFeatureObjectIdentity) – pavan Jun 17 '15 at 12:33
  • Yes: support was added after I added my comment. Thank you for noting the "new" feature (was added in 2.0). – StaxMan Jun 17 '15 at 21:12