5

I have a bidirectional relationship

@Entity
@Table(name = "facility")
public class Facility implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy = "facility")
    private Set<Amenity> amenities;
}

@Entity
@Table(name = "amenity")
public class Amenity  implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    private Facility facility;
}

This all works fine and I can see the table is created correctly. I can add data fine through a rest endpoint and but it was only when I wanted to go in the opposite direct and get the facility for an amenity when I got this issue. Its look like its going into an infinite loop, facility calls amenity calls facility calls amenity and so on. Its like there is some circular reference. I've have searched high and low , far and wide and followed many examples and all seem to have entities set up in a similar fashion. I have left out getters and setters for brevity. I would expect after it got its first amenity it would stop.

[
    {
        "id": 1,
        "facilityName": "asdf",
        "facilityCode": "asdf",
        "amenities": [
            {
                "id": 15,
                "amenityType": "amenity 1",
                "facility": {
                    "id": 1,
                    "facilityName": "asdf",
                    "facilityCode": "asdf",
                    "amenities": [
                        {
  • Of course there is a circular reference, each entity refers to the other. What are you using to traverse the object tree? Whatever code you are using to build up the JSON object needs to know not to go back 'up' your tree of objects. Can you explain more about how that works? – DuncanKinnear May 24 '15 at 20:51

0 Answers0