[英]How to implement a read-only property with Spring Data?
This should be a simple thing to do! 这应该是一件简单的事情! But I've been unable to find an answer so far.
但到目前为止我一直无法找到答案。 Either I'm missing something obvious, or else I'm missing something obvious...
要么我错过了一些明显的东西,否则我会遗漏一些明显的东西......
I have a class, say Person. 人说,我有一节课。 With three fields - "id", "name" and "reputation".
有三个字段 - “id”,“name”和“reputation”。 Let's say that I am willing to take updates to "name" but not to "reputation".
让我们说我愿意更新“名称”而不是“声誉”。 I'd like Spring Data to fetch the value of "reputation" when retrieving from DB, but ignore it when I'm saving the bean.
我希望Spring Data在从DB中检索时获取“信誉”的值,但在保存bean时忽略它。
@Transient annotation is there, but then Spring completely ignores the field and doesn't populate it at all. @Transient注释就在那里,但是Spring完全忽略了该字段并且根本没有填充它。 Ideally, I'm looking for something like @ReadOnly annotation.
理想情况下,我正在寻找类似@ReadOnly注释的东西。
More details 更多细节
You can use a transient property without a setter. 您可以在没有setter的情况下使用transient属性。 That transient property would return the db property value that is to be protected.
该transient属性将返回要保护的db属性值。
you can use @ReadOnlyProperty from org.springframework.data.annotation. 你可以使用org.springframework.data.annotation中的@ReadOnlyProperty。 see ReadOnlyProperty
请参阅ReadOnlyProperty
@ReadOnlyProperty
private Object readOnlyValue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.