Dmitry Serebrennikov
Dmitry Serebrennikov

Reputation: 139

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". 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.

@Transient annotation is there, but then Spring completely ignores the field and doesn't populate it at all. Ideally, I'm looking for something like @ReadOnly annotation.

More details

Upvotes: 8

Views: 9705

Answers (2)

xyz
xyz

Reputation: 5407

you can use @ReadOnlyProperty from org.springframework.data.annotation. see ReadOnlyProperty

@ReadOnlyProperty
private Object readOnlyValue;

Upvotes: 2

jagra
jagra

Reputation: 553

You can use a transient property without a setter. That transient property would return the db property value that is to be protected.

Upvotes: 2

Related Questions