spring-projects / spring-projects/spring-data-rest
Circular JPA entity references cause infinite recursion in LinkCollectingAssociationHandler/BasicPersistentEntity [DATAREST-396]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- 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:
- AssociationBug.java (6.88 kB)
- LinkCollectingAssociationHandler.java.patch (1.29 kB)
10 votes, 11 watchers
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.