6

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

  • I'm using Spring Data for Neo4j, but I believe this would apply to any Spring Data flavor.
  • This is a backend for Jersey/Jackson-based RESTful service. ** When I satisfy the GET request, I'd like to serve the "reputation" value. But when I receive a PUT update I don't want to take it. ** So far I could use Jackson features. But I'd like to be able to update the DB without having to fetch the existing Person object first.
  • The only way I can figure for making this work is to define two classes - one with "reputation" field and one without. But this seems really clunky. Isn't there something simpler?
4

2 回答 2

2

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

于 2013-03-30T02:55:12.213 回答
1

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

@ReadOnlyProperty
private Object readOnlyValue;
于 2017-06-21T17:49:21.510 回答