Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map OneToOne relationship between POJOs using @JsonManagedReference and @JsonBackReference

I have read

  • http://vard-lokkur.blogspot.com/2010/10/json-jackson-to-rescue.html
  • http://vard-lokkur.blogspot.com/2010/10/json-jackson-serialization-narrowed.html
  • http://www.cowtowncoder.com/blog/archives/2010/09/entry_418.html

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?

like image 388
Steinway Wu Avatar asked Jul 18 '11 16:07

Steinway Wu


1 Answers

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.

like image 116
StaxMan Avatar answered Sep 19 '22 17:09

StaxMan