Elasticsearch - 忽略属性的持久化

时间:2015-11-07 13:06:10

标签: java spring elasticsearch spring-data spring-data-elasticsearch

我有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;
    }
    }

1 个答案:

答案 0 :(得分:0)

有一种方法可以自定义要包含在JsonView的json中的内容,如Ignore JsonIgnore in Elasticsearch

您可以创建两个视图,一个用于Rest,另一个用于Elasticsearch,并用所需的视图标记该字段。并告诉Elasticsearch的ObjectMapper仅与特定的JsonView一起使用