英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论