This tutorial will walk you through the steps of creating an example on handling circular references/dependencies of JPA/Hibernate entity bidirectional relationships with Jackson JsonIgnoreProperties, CRUD RESTful APIs, Spring Data REST, MySQL and Docker.

JPA/Hibernate Entity Bidirectional Relationship Mapping Example Series with Spring Boot and MySQL
- JPA/Hibernate Composite Primary Key Example
- JPA/Hibernate One To One Bidirectional Relationship Mapping
- JPA/Hibernate One To One Shared Primary Key Bidirectional Relationship Mapping
- JPA/Hibernate One To Many Bidirectional Relationship Mapping
- JPA/Hibernate Many To Many Bidirectional Relationship Mapping
- JPA/Hibernate Many To Many Extra Columns Bidirectional Relationship Mapping
- Handling Circular Reference of JPA/Hibernate Entity Bidirectional Relationships with Jackson JsonIgnoreProperties
- JPA/Hibernate CRUD Example, Deleting Data with JPQL, CascadeType, and orphanRemoval
- Database Migration/Evolution Example with Flyway and JPA/Hibernate

What you'll need

  • Docker CE 18+

Project structure

Project dependencies

Define JPA Entities Bidirectional Relationships with Jackson circular references handling

Jackson @JsonIgnoreProperties will prevent specified fields from being serialized or deserialized.

Lombok @EqualsAndHashCode with exclude will ignore specified fields on the generated equals and hashCode function of Lombok @Data

Define Repository

Spring Data REST will auto create RESTful APIs based on your domain model and repository.

Application Properties

Run the application

Prepare Dockerfile for Java/Spring Boot application

Start your application and infrastructure via Docker Compose. Make sure your local Docker is running and your bash console is at the springboot-examples project root directory.

docker-compose -f docker-compose-restful.yaml up --renew-anon-volumes circular-reference-jackson

Test circular references handling and RESTful APIs with curl

Create a new book

curl -i -X POST -H "Content-Type:application/json" -d "{\"title\" : \"Hello Koding\", \"description\": \"Simple coding examples and tutorials\"}" http://localhost:8080/books 

Create a new author

curl -i -X POST -H "Content-Type:application/json" -d "{\"name\":\"Author 1\"}" http://localhost:8080/authors

Assign the author to the book

curl -i -X PUT -H "Content-Type:text/uri-list" -d "http://localhost:8080/authors/1" http://localhost:8080/books/1/authors

Find all books

curl http://localhost:8080/books

Find authors of book id 1

curl http://localhost:8080/books/1/authors

Find books of author id 1

curl http://localhost:8080/authors/1/books

That's it. Happy learning!

Source code