Bi-Directional Relationships
A bi-directional relationship is very common in JPA, the persistence architecture used in the Play! Framework. Most @ManyToOnes complement a @OneToMany allowing for different navigation paths. For example, this @Entity Topic may classify one or more Tutorials.
@OneToMany Part of Bi-Directional Relationship |
@ManyToOne Part of Bi-Directional Relationship |
Circular Reference Error |
Entity that Initiated Reference Error |
The error is generated because the default JSON serializer can't resolve the circular reference. For a good explanation of the default behavior and an alternative resolution to the above error, follow this link to Lunatech Research.
JsonSerializer
To resolve the circular reference error, create your own JsonSerializer. Your custom JsonSerializer should restrict the navigation to a single path. For example, this JsonSerializer outputs an @Entity TextualProc and its child records Step. TextualProc and Step have a bi-directional relationship, but it isn't being considered as the for loop navigates one-way.
A JsonSerializer |
JSON Result |
Controller
Instead of using a renderJSON() call with a single object argument in the Play! Controller, the call will use an additional argument for the JsonSerializer. Here is a method from a Controller.
Controller Method Rendering JSON |
It's an old post but THANK YOU!
ReplyDelete