title: Make field insertable but not updatable in Spring Data REST + MongoDB
body: <p>With Spring Data REST and Spring Data Mongo, I want to make a domain field (field <code>username</code> of domain <code>User</code> in my example) <strong>insertable</strong> when create but <strong>un-updatable</strong> when update. In other words an equivalent of JPA @Column(insertable = true, updatable = false).</p>
  
  <p>I try a few approach but not work.
  In my <a href="https://github.com/yejianfengblue/spring-data-rest-mongo-field-non-updatable" rel="noreferrer">github project</a>, domain class and repository are put in /src/main/java/*/*/User.java and UserRepository.java. The test is put in /src/test/java/*/*UserTest.java.</p>
  
  <h2>1. Spring Data annotation @ReadOnlyProperty and @Transient</h2>
  
  <p>The field is un-insertable when save to DB. See package <code>readonlyproperty</code> and <code>transient_</code> in the project.</p>
  
  <h2>2. Jackson annotation @JsonProperty(access=READ_ONLY)</h2>
  
  <p>The field is un-insertable when create via POST request, because the JSON property is ignored when initiate an object. See package <code>jsonpropertyreadonly</code> in the project.</p>
  
  <h2>3. @JsonCreator on constructor and @JsonIgnore on setter</h2>
  
  <p>If the un-updatable field <code>username</code> is contained in json body of PUT or PATCH request, and <code>username</code> value changes, <code>username</code> get updated, which is unexpected. See package <code>jsoncreator</code> in the project.</p>
  
  <h2>4. Do not write a setter</h2>
  
  <p>same as 3. See package <code>nosetter</code> in the project.</p>
  
  <h2>5. Toggle on/off feature</h2>
  
  <pre><code>spring.jackson.deserialization.fail-on-ignored-properties=false
  spring.jackson.deserialization.fail-on-unknown-properties=false
  spring.jackson.mapper.infer-property-mutators=false
  </code></pre>
  
  <p>not help</p>
  
  <h2>Spring Data REST PUT and PATCH Internal Implementation</h2>
  
  <ul>
  <li>PUT: it uses Jackson <code>ObjectMapper.readerFor(Class)</code> to initiate a new object</li>
  <li>PATCH: it uses Jackson <code>ObjectMapper.readerForUpdating(objectToUpdate).readValue(json)</code>, which use setter to update the <code>objectToUpdate</code>. Seems <code>readerForUpdating</code> doesn't see the @JsonIgnore on setter.</li>
  </ul>
  
  <p>The only solution I know is implementing the setter in below way</p>
  
  <pre class="lang-java prettyprint-override"><code>void setUsername(String usernameToSet) {
      if (null == this.username)
          this.username = usernameToSet;
  }
  </code></pre>
  
  <p>And disable PUT method, only use PATCH to update. See package <code>setterchecknull</code>.</p>
  
  <p>Is there a better way? Thank you very much!!</p>