2017-12-04 7 views
1

私はクラス間で春に数多くの関係を1対1持っています。そして、私がログインしようとすると、エラーが発生します。無限の再帰のようなものです。エラーメッセージ全体です。com.fasterxml.jackson.databind.ser.BeanSerializer.serialize Spring JPA

at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.10.jar:2.8.10] 
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) ~[jackson-databind-2.8.10.jar:2.8.10] 

私は互いに接続された5つのクラスを持っています。 ここで彼らは、次のとおりです。

@Entity 
public class AppUser implements UserDetails { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 
    private String name; 

    @Column(unique = true) 
    private String username; 

    private String password; 

    @ElementCollection(fetch = FetchType.EAGER) 
    private List<String> roles = new ArrayList<>(); 

    @ManyToOne 
    @JoinColumn(name = "apartmen_id") 
    @JsonIgnoreProperties(value = {"apartmen_tenats"}, allowSetters=true) 
    private Apartmen apartmen; // apartmen in which he lives 

    @ManyToOne 
    @JoinColumn(name = "institution_id") 
    @JsonIgnoreProperties(value = {"workers"}, allowSetters=true) 
    private Institution institution; // institution in which he works 

    @OneToMany(mappedBy = "worker") 
    @JsonIgnoreProperties(value = {"worker"}, allowSetters = true) 
    private Set<Failure> failures; // Kvarovi na kojima je radio 
} 

番目のクラス:

@Entity 
public class Failure implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    private String name; 
    private String description; // Opis kvara 

    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateCreated; // Datum i vreme kada je kvar kreiran 

    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateSolved; // Datum i vreme kada je kvar popravljen 

    private boolean solved; // Da li je kvar popravljen 

    @ManyToOne 
    @JoinColumn(name = "app_user_id") 
    @JsonIgnoreProperties(value = {"failures"}, allowSetters=true) 
    private AppUser worker; // Radnik koji je zaduzen za kvar 

    @ManyToOne 
    @JoinColumn(name = "institution_id") 
    @JsonIgnoreProperties(value = {"failures"}, allowSetters=true) 
    private Institution institution; // Institucija kojoj je kvar prijavljen 

    @ManyToOne 
    @JoinColumn(name = "building_id") 
    @JsonIgnoreProperties(value = {"failures"}, allowSetters=true) 
    private Building building; // Zgrada u kojoj je kvar nastao 
} 

第三のクラス:

@Entity 
public class Building implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    private String name; 
    private String location; 
    private String owner; // Vlasnik zgrade 
    private int numberOfApartments; 
    private int numberOfAparartmentsWithTenats; // Broj stanova koji su naseljeni 
    private boolean hasPresident; // Oznacava da li zgrada ima predsednika skupstine stanara 

    @OneToMany(mappedBy = "apartmenBuilding") 
    @JsonIgnoreProperties(value = {"apartmenBuilding"}, allowSetters = true) 
    private Set<Apartmen> apartments; // stanovi u zgradi 

    @ManyToMany(mappedBy = "buildings") 
    @JsonIgnoreProperties(value = {"buildings"}, allowSetters = true) 
    private Set<Institution> institutions; 

    @OneToMany(mappedBy = "building", fetch = FetchType.EAGER) 
    @JsonIgnoreProperties(value = {"building"}, allowSetters = true) 
    private List<Failure> failures; // Kvarovi koji su nastali u zgradi 
} 

第4のクラス:

@Entity 
public class Institution { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    private String name; 
    private String location; 
    private String director; 
    private String email; 
    private String contactPhone; 
    private String webSiteUrl; 

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE) 
    @JoinTable(name = "institution_building", 
     joinColumns = @JoinColumn(name = "institution_id", referencedColumnName = "id"), 
     inverseJoinColumns = @JoinColumn(name = "building_id", referencedColumnName = "id")) 
    @JsonIgnoreProperties(value = {"institutions"}, allowSetters=true) 
    private Set<Building> buildings; // Buildings which this institution is maintaining 

    @OneToMany(mappedBy = "institution") 
    @JsonIgnoreProperties(value = {"institution"}, allowSetters = true) 
    private Set<AppUser> workers; 

    @OneToMany(mappedBy = "institution", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @JsonIgnoreProperties(value = {"institution"}, allowSetters = true) 
    private Set<Failure> failures; // Kvarovi na kojima je radila institucija 
} 

そして、最後のクラスです:

@Entity 
public class Apartmen implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    private String name; 
    private String location; 
    private String owner; 
    private int numberOfTenats; 
    private boolean hasApartmentBuilding; 
    private boolean hasOwner; 

    @ManyToOne 
    @JoinColumn(name = "building_id") 
    @JsonIgnoreProperties(value = {"apartments"}, allowSetters=true) 
    private Building apartmenBuilding; 

    @OneToMany(mappedBy = "apartmen") 
    @JsonIgnoreProperties(value = {"apartmen"}, allowSetters = true) 
    private Set<AppUser> apartmen_tenats; 
} 

私はそれがなぜ起こるのかは分かりませんが、クラスFailureとの関係のためかもしれません。そのクラスは私のコード内の他のすべてのクラスとの関係を持っているので、そうかもしれないと思います。それが事実なら、なぜあなたはそれが起こっているのか、それを正しく行う方法を私に見せてください。

ありがとうございます。

+0

JPA APIではなく、基本的にJSONシリアライゼーションです。全く異なるプロセス – DN1

答えて

1

@JsonIgnorePropertiesは、クラスレベルの注釈であり、クラス内の正確なフィールド名が無視されることを期待しています。

最良の方法は、すべてのあなたの@JsonIgnoreProperties@JsonIgnorePropertiesのようなすべての出現のために同様の変更を行い

@ManyToOne 
@JoinColumn(name = "apartmen_id") 
@JsonIgnore 
private Apartmen apartmen; 

単に

@JsonIgnore. 

すなわち

@ManyToOne 
@JoinColumn(name = "apartmen_id") 
@JsonIgnoreProperties(value = {"apartmen_tenats"}, allowSetters=true) 
private Apartmen apartmen; 

変更これで置き換えることです。 これは動作するはずです。