问题
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": [
{
来源:https://stackoverflow.com/questions/30423259/jpa-bidirectional-relationship-infinite-loop-circular-reference