spring-projects / spring-projects/spring-data-rest

Circular JPA entity references cause infinite recursion in LinkCollectingAssociationHandler/BasicPersistentEntity [DATAREST-396]

Open
#773 9 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

has: votes-jira in: repository status: feedback-provided type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Balázs E. Pataki opened DATAREST-396 and commented

I have an entity that has a property of the same type as the entity itself.

Something like:

@Entity
public static class Location {  

  // NOTE: circular type reference
  @ManyToOne
  @JoinColumn(name="partOf")
  private Location partOf;
  …
}

When I create a Repository for it and try to use the exported REST service to access an instance of this entity, or any entity that references this entity will cause an infinite recursion like this:

at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:100)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:337)
at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:337)
at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:337)
at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:337)
at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:337)
at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)

It also happens when the partOf is not even set. I attached a simple test case, which shows this happening. Since this is not a JSON (Jackson) related bug @JsonIgnore cannot be used here. This seems to be some weird operation of the hypermedia link (association) generation functions.


Affects: 2.1.4 (Dijkstra SR4)

Attachments:

10 votes, 11 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.