検索中に見つけたいくつかのことを試しましたが、何も助けなかったし、正しく実装しなかった。サイクルにつながる直接自己参照スーパークラスの問題JSON
エラー私は、それは同様appInstance
を持っているこれらの両方のは、基本エンティティを拡張し、ベース(スーパークラス)に
Direct self-reference leading to cycle (through reference chain: io.test.entity.bone.Special["appInstance"]->io.test.entity.platform.ApplicationInstance["appInstance"])
を取得しています。
基本エンティティが AppInstance
含まれている特別で@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType")
@JsonIgnoreProperties({"createdBy", "appInstance", "lastUpdatedBy"})
public class Special extends BaseEntity implements Serializable {
@NotNull
@Column(nullable = false)
private String name;
@Column(length = Short.MAX_VALUE)
private String description;
@NotNull
@Column(nullable = false)
private Double price;
@OneToOne
private Attachment image;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = SpecialTag.class)
@CollectionTable(name = "special_tags")
@Column(name = "specialtag")
private List<SpecialTag> specialTags;
@Temporal(TemporalType.TIME)
private Date specialStartTime;
@Temporal(TemporalType.TIME)
private Date specialEndTime;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = WeekDay.class)
@CollectionTable(name = "available_week_days")
@Column(name = "weekday")
private List<WeekDay> availableWeekDays;
@OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
private List<SpecialStatus> statuses;
@OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
private List<SpecialReview> specialReviews;
@Transient
private Integer viewed;
private Boolean launched;
@OneToMany(mappedBy = "special")
private List<CampaignSpecial> specialCampaigns;
@Override
@JsonIgnore
public ApplicationInstance getAppInstance() {
return super.getAppInstance();
}
}
すべてのエンティティがBaseEntityから継承この
public class ApplicationInstance extends BaseEntity implements Serializable {
private List<User> users;
// some other properties (would all have the same base and application instance . User entity will look similar to the Special.)
}
特別実体のように見えるこの
@MappedSuperclass
public abstract class BaseEntity implements Comparable, Serializable {
@ManyToOne
protected ApplicationInstance appInstance;
//getter & setter
}
アプリケーションエンティティに似ています
その後、私は、次の
- 追加jsonIgnoreProperties
- が@JsonIgnore
- に注釈を付けることappInstanceの上書きを追加しました試みた特別なエンティティで特別
@GET @Path("{ref}") @Produces(MediaType.APPLICATION_JSON) @Consumes(value = MediaType.TEXT_PLAIN) public Special findByGuestRef(@PathParam("ref") String pRefeference) { // find the special and return it return special; }
を取得する方法を持っています@JsonIdentityInfo
abovのリンクこれらのソリューションの作品の電子
- https://stackoverflow.com/a/29632358/4712391
- Jackson serialization: how to ignore superclass properties
- jackson self reference leading to cycle
なし。私は何か間違っているのですか?
注:他のエンティティが異なるパッケージにあり、編集したくないため、特別な編集もできますか?
'protected ApplicationInstance appInstance'をプライベートにして、それに対していくつかのゲッターとセッターを追加します(protectedまたはpublicのいずれか)。次に、' @JsonIgnore'アノテーションで 'ApplicationInstance'のゲッターをオーバーライドします。 – ConductedClever
@aristotllもう少し詳細を追加しましたが、変更されるとは思いません。アプリケーションインスタンス –
を参照してください。appInstanceをこのインスタンスからのみ除外するので、マップされたクラスに '@ JsonIgnore'を追加できないのはなぜですか? – Mark