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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Introduction To Git
  • Never Use Credentials in a CI/CD Pipeline Again

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Introduction To Git
  • Never Use Credentials in a CI/CD Pipeline Again
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Circular Dependencies With Jackson

Circular Dependencies With Jackson

Lieven Doclo user avatar by
Lieven Doclo
·
Jul. 25, 13 · Interview
Like (0)
Save
Tweet
Share
27.91K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Introduction To Git
  • Never Use Credentials in a CI/CD Pipeline Again

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: