Skip to content

Bidirectionnal relations go in infinite loop during check #633

@robindecath

Description

@robindecath

Hello,
It's been several times I go through this error :
I have two entities that know each other :

@Entity
public class CbdHardwareEntity {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   
   /* other properties */

   @OneToOne(mappedBy = "cbd", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
   private CbdHardwareBomEntity bom;
}
@Entity
public class CbdHardwareBomEntity {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   
   /* other properties */

   @OneToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "fk_cbd_id", referencedColumnName = "id", columnDefinition = "integer")
   private CbdHardwareEntity cbd;
}

And my tzatziki tests :

    And that the CbdHardwareEntity entities will contain:
    """yml
    - conceptionCode: 123456
    """
    And that the CbdHardwareBomEntity entities will contain:
    """yml
    - name: bom pace name
       perimeter: perimeter pace
       paceId: 50
       cbd.id: 1
    """

When I do a And the CbdHardwareEntity entities contain: to check the content of my main table after I called my service, it goes into an infinite loop :

com.fasterxml.jackson.databind.JsonMappingException: Document nesting depth (1001) exceeds the maximum allowed (1000, from `StreamWriteConstraints.getMaxNestingDepth()`) (through reference chain: com.decathlon.buysmart.datacost.hardware.repository.models.CbdHardwareEntity["bom"]->com.decathlon.buysmart.datacost.hardware.repository.models.CbdHardwareBomEntity["cbd"]->com.decathlon.buysmart.datacost.hardware.repository.models.CbdHardwareEntity["bom"]->com.decathlon.buysmart.datacost.hardware.repository.models.CbdHardwareBomEntity["cbd"]->com.decathlon.buysmart.datacost.hardware.repository.models.CbdHardwareEntity["bom"]->com.decathlon.buysmart.datacost.hardware.repository.models.CbdHardwareBomEntity["cbd"]-

I have tried the @JsonManagedReference and @JsonBackReference, it did work but I prefer not to modify my entities just to make tests work.

Do you have a solution to propose if I ever met this problem ? I will call my api to get resources to make the check I need; but it make more sense to directly check the content of a table.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions