DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Securing CI/CD Pipelines Against Supply Chain Attacks: Why Artifacts and Dependencies Matter More Than Ever
  • Clean Code: Package Architecture, Dependency Flow, and Scalability, Part 4
  • C/C++ Is Where Vulnerability Programs Go to Guess
  • Tracking Dependencies Beyond the Build Stage

Trending

  • Spring Boot Done Right: Lessons From a 400-Module Codebase
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • AI-Driven Integration in Large-Scale Agile Environments
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Circular Dependencies With Jackson

Circular Dependencies With Jackson

By 
Lieven Doclo user avatar
Lieven Doclo
·
Jul. 25, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
30.5K Views

Join the DZone community and get the full member experience.

Join For Free

Circular dependencies and JSON have always been a pain. But it’s not just JSON, the problem also exists when you’re trying to serialize a graph which contains circular dependencies (parent/child with bidirectional relationships).

Some time ago, we were considering exposing our JPA datamodel through REST services. Off course, a lot of JPA model classes contain bidirectional relationships, which was a real pain to get working. We ended up with a separate data model consisting of DTO’s (yuck!) and a mapping between the two models. But after a while we had to abandon our REST quest due to the fact that the JPA data model was getting to complicated. So we let go of the loose coupling between the client and the server, which made the issue go away completely. REST services where built when the need for external communication arose, but for client-server communication a more direct dependency was used (CDI/EJB or Spring injection).

Recently, I once again looked at Jackson. My reasons now where a bit different. Our data model has grown to a point where finding out what exactly is in a graph is getting problematic. A simple SQL query doesn’t cut it anymore and we’re forced to start debugging in order to see what an object actually contains. Knowing in advance how a complex JPA datamodel is populated through a JQPL query is a science on its own.

So I thought, why not have the possibility to send the same JPQL query and have the result returned to us as JSON. The problem, I thought, would be those wretched circular dependencies. Luckily, the Jackson developers have since developed a solution to the problem: their JSON serializer now supports object references. And it’s usable out-of-the-box for JPA datamodels.

Their JSON object reference requires an object to have a unique ID. Luckily, this is also the case for JPA entities. However, JSON id references need to be unique across the entire graph, whereas JPA id’s only need to be unique within the same entity. In our case, it wasn’t really an issue, as we use UUID’s for JPA id fields, which are unique throughout the entire database.

So how do you serialize an object graph? Well, assume you have two entities with bidirectional relationships like this:

@Entity
public class ParentEntity {
    @Id
    private String id;
    private String description;
    @OneToMany(mappedBy = "parent")
    private List<ChildEntity> children;

     // getters and setters omitted for brevity
}

@Entity
public class ChildEntity {
    @Id
    private String id;
    private String description;
    @ManyToOne
    private Parent parent;

    // getters and setters omitted for brevity
}

Adding Jackson JSON identities is very simple:

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class ParentEntity {
    ...
}

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class ChildEntity {
    ...
}

And that’s it! If you would now serialize a parent object with 2 children, you’ll get something like this:

{
    "id": "parent-id1",
    "description": "parent",
    "children": [
        {
            "id": "child-id1",
            "description": "child1",
            "parent": "parent-id1"
        },
        {
            "id": "child-id2",
            "description": "child2",
            "parent": "parent-id1"
        }
    ]
}
Dependency Jackson (API)

Published at DZone with permission of Lieven Doclo. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Securing CI/CD Pipelines Against Supply Chain Attacks: Why Artifacts and Dependencies Matter More Than Ever
  • Clean Code: Package Architecture, Dependency Flow, and Scalability, Part 4
  • C/C++ Is Where Vulnerability Programs Go to Guess
  • Tracking Dependencies Beyond the Build Stage

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook