Ignore property when persisting to ES, don’t ignore when persisting to DB.

huangapple go评论64阅读模式
英文:

Ignore property when persisting to ES, don't ignore when persisting to DB

问题

You can use @Transient from the javax.persistence package to exclude the property from Elasticsearch while persisting it in a PostgreSQL database. Here's the modified code:

@Entity
@Table(name = "example")
@Document(indexName = "example")
public class Example implements Serializable {

  @Column(name = "my_date")
  @Transient
  private Instant myDate;

}

Using @Transient will indicate that the myDate property should not be persisted in Elasticsearch while allowing it to be persisted in the PostgreSQL database.

英文:

Using spring-data(-elasticsearch), I want to persist a property to a PSQL DB but not to Elasticsearch, i.e. the opposite to this question: https://stackoverflow.com/questions/71843478/spring-data-how-to-ignore-field-for-relational-database-but-not-in-elasticsearc

In other words:

  • when inserting this entity into ES, ignore myDate
  • when inserting this entity into a DB, persist myDate
@Entity
@Table("example")
@Document(indexName = "example")
public class Example implements Serializable {
  
  @Column(name = "my_date")
  @xyz
  private Instant myDate;

}

What should take the place of @xyz ? Is this possible at all?

  • @org.springframework.data.annotation.Transient excludes the property from both PSQL and ES
  • @JsonIgnore does nothing

答案1

得分: 1

不要在多个Spring Data模块中使用相同的实体。正如您注意到的那样,这是一次意外等待发生的事情。

您可以实现一个BeforeConvertCallback(参见参考文档)并将myDate属性设置为null,这样它就不会被发送到Elasticsearch。

英文:

Don't use the same entity for multiple Spring Data modules. As you notice, this is an accident waiting to happen.

You could implement a BeforeConvertCallback (see the reference documentation) and set the myDate property to null, so it won't be sent to Elasticsearch.

huangapple
  • 本文由 发表于 2023年5月22日 23:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定