How to implement a read-only property using Spring data?

It should be a simple thing! But so far I could not find the answer. Either I'm missing something obvious, or I'm missing something obvious ...

I have a class, say Personality. With three fields - "id", "name" and "reputation". Let's say that I'm ready to accept updates for the "name", but not for the "reputation." I would like Spring Data to retrieve the β€œreputation” value when retrieving from the database, but ignore it when I save the bean.

There is @Transient annotation, but then Spring completely ignores the field and does not fill it at all. Ideally, I'm looking for something like @ReadOnly annotations.

More details

  • I use Spring Data for Neo4j, but I believe this applies to any flavor of Spring data.
  • This is the backend for the RESTful service in Jersey / Jackson. ** When I satisfy the GET request, I would like to use the value "reputation". But when I receive the PUT update, I do not want to receive it. ** So far, I have been able to use Jackson's features. But I would like to be able to update the database without first using the Person object.
  • The only way I can count on this work is to define two classes: one with the reputation field and one without it. But that seems really awkward. Isn’t it easier?
+5
source share
2 answers

You can use the transient property without a setter. This transient property will return the value of the db property that should be protected.

+2
source

@ReadOnlyProperty org.springframework.data.annotation. . ReadOnlyProperty

@ReadOnlyProperty
private Object readOnlyValue;
0

All Articles