【发布时间】:2016-02-08 13:30:03
【问题描述】:
我有要持久化到数据库的 bean,我也想将它们持久化到 elasticsearch。但是我有一个问题,例如,在国家豆中有城市的集合,每个城市都有国家档案。当我尝试将其保存到 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;
}
}
【问题讨论】:
-
您要忽略哪个字段?为什么不添加到
@JsonIgnoreProperties? -
我想忽略 CountryEntity 中的城市字段。那么,我将无法通过 REST 将其返回给客户端应用程序。
-
好的,我现在明白了。您想使用相同的 bean 和相同的映射器,但根据端点忽略某些字段?抱歉,帮不上忙。
标签: java spring elasticsearch spring-data spring-data-elasticsearch