我有一些要持久保存到数据库的bean,我也想将它们持久保存到elasticsearch。但我有一个问题,例如,在乡村 bean 中有城市的集合,每个城市都有国家文件。当我尝试将其保存到 elasticsearch 时,出现异常: com.fasterxml.jackson.databind.JsonMappingException:无限递归
有没有办法标记归档不保留到elasticsearch,我尝试使用@Transient,但它对我没有帮助。
@JsonInclude 不是一个选项,因为我也使用这个 bean 通过 REST 将信息返回给客户端。
@Document(indexName = "cities", type = "city", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
@Entity
@Table(name = "CITY")
@SQLDelete(sql = "UPDATE CITY SET deleted = 1 WHERE City_id = ? AND version=?")
@Where(clause = "deleted = 0")
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler", "$resolved", "$promise", "deleted" })
public class CityEntity implements Serializable {
private static final long serialVersionUID = -5696224598424579197L;
@org.springframework.data.annotation.Id
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "City_id")
protected Long id;
@Version
protected int version;
@Field(type = FieldType.Boolean, store = false)
protected boolean deleted;
@Field(type = FieldType.String, store = true)
@Column(name = "code")
protected String code;
@Field(type = FieldType.String, store = true)
@Column(name = "name", nullable = false)
protected String name;
@Field(type = FieldType.Nested, store = false)
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinColumn(name = "country")
protected CountryEntity country;
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(final Long newId) {
id = newId;
}
@JsonProperty("deleted")
public Boolean getDeleted() {
return deleted;
}
public void setDeleted(final Boolean newDeleted) {
deleted = newDeleted;
}
@JsonProperty("version")
public int getVersion() {
return version;
}
public void setVersion(final int newVersion) {
version = newVersion;
}
@JsonProperty("code")
public String getCode() {
return code;
}
public void setCode(final String newcode) {
code = newcode;
}
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(final String newname) {
name = newname;
}
@JsonProperty("country")
public CountryEntity getCountry() {
return country;
}
public void setCountry(final CountryEntity newcountry) {
country = newcountry;
}
}
@Document(indexName = "countries", type = "country", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
@Entity
@Table(name = "COUNTRY")
@SQLDelete(sql="UPDATE COUNTRY SET deleted = 1 WHERE Country_id = ? AND version=?")
@Where(clause = "deleted = 0")
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler","$resolved","$promise", "deleted" })
public class CountryEntity implements Serializable {
private static final long serialVersionUID = -1804208351747583363L;
@org.springframework.data.annotation.Id
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Country_id")
protected Long id;
@Version
protected int version;
protected boolean deleted;
@Field(type = FieldType.String, store = true)
@Column(name = "code", nullable = false)
protected String code;
@Field(type = FieldType.String, store = true)
@Column(name = "name", nullable = false)
protected String name;
@Field(type = FieldType.Nested, store = false)
@OneToMany(mappedBy = "country", fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
@Where(clause = "deleted = 0")
protected Set<CityEntity> cities = new HashSet<CityEntity>();
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long newId) {
id = newId;
}
@JsonProperty("deleted")
public Boolean getDeleted() {
return deleted;
}
public void setDeleted(Boolean newDeleted) {
deleted = newDeleted;
}
@JsonProperty("version")
public int getVersion() {
return version;
}
public void setVersion(int newVersion) {
version = newVersion;
}
@JsonProperty("code")
public String getCode() {
return code;
}
public void setCode(String newcode) {
code = newcode;
}
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String newname) {
name = newname;
}
@JsonProperty("cities")
public Set<CityEntity> getCities() {
return cities;
}
public void setCities(Set<CityEntity> newcities) {
cities = newcities;
}
}
最佳答案
有一种方法可以自定义 JsonView 包含到 json 中的内容,如此处所述:Ignore JsonIgnore in Elasticsearch
您可以创建两个 View ,一个用于 Rest,另一个用于 Elasticsearch,并用所需的 View 标记该字段。并告诉 Elasticsearch 的 ObjectMapper 仅使用特定的 JsonView
关于java - Elasticsearch-忽略持久化的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33583097/