8

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?
  • I've been digging around and it seems like this would be quite easy to add. For example, there is already ReadOnlyRelatedToCollectionFieldAccessorFactory. I just need something like that for node properties. I'm puzzled why this isn't already there though. – Dmitry Serebrennikov Mar 30 '13 at 01:22
  • I know with Spring Data and JPA, this is handled with a JPA annotation. I'm completely unfamiliar with Neo4J but I'd start but looking into Neo4j. – Taylor Mar 30 '13 at 03:04

2 Answers2

2

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

jagra
  • 543
  • 2
  • 8
2

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

@ReadOnlyProperty
private Object readOnlyValue;
xyz
  • 5,228
  • 2
  • 26
  • 35