title: @Version - Spring boot
body: <p>I've been trying to get <code>@Version</code> to work but I can't seem to do it, I'm building a <strong>Rest API</strong>, when I try to edit I want to make sure that the version that the user is editing is the current one and not one that was opened and edited by other person, for that I used <strong><em><code>@javax.persistence.Version</code></em></strong>, </p>
  
  <p>This is currently my controller code:</p>
  
  <pre><code>@InitBinder("song")
  protected void initBinder(WebDataBinder binder) {
      binder.setValidator(new SongValidator());
  }
  
  @PutMapping("{song}")
  public ResponseEntity&lt;?&gt; update(@PathVariable("song") int songId, @Valid @RequestBody Song song) {
      Song currentSong = songService.findSongByIdAndDeletedAtIsNull(songId);
      if (currentSong == null) {
          return new ResponseEntity(new CustomErrorType(
                  "Song with id " + songId + " not found."
          ), HttpStatus.NOT_FOUND);
      }
  
      currentSong.setName(song.getName());
      currentSong.setLyrics(song.getLyrics());
      currentSong.setTypes(song.getTypes());
  
      songService.save(currentSong);
      return new ResponseEntity&lt;Song&gt;(currentSong, HttpStatus.OK);
  }
  </code></pre>
  
  <p>On the model I have this:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;
  
  private String name;
  
  private String slug;
  
  @Lob
  private String lyrics;
  
  @CreationTimestamp
  private Date createdAt;
  
  private Date updatedAt = new Date();
  
  private Date deletedAt;
  
  @OrderBy("id")
  @ManyToMany
  @JoinTable(joinColumns = @JoinColumn(name = "song_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "types_id",  referencedColumnName = "id"))
  private List&lt;SongType&gt; types;
  
  @JsonProperty(access = Access.READ_ONLY)
  @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "name")
  @JsonIdentityReference(alwaysAsId=true)
  @ManyToOne
  private User user;
  
  @javax.persistence.Version
  private int version;
  
  public int getVersion() {
      return version;
  }
  
  public void setVersion(int version) {
      this.version = version;
  }
  ... other getters and setters ...
  </code></pre>
  
  <p>This was the json I used to update the song:</p>
  
  <pre><code>{
      "id": 24
      "name": "New song for Update test",
      "slug": "new-song-for-update-test",
      "lyrics": "lyrics for the song update",
      "types": [
          {
              "id": 2
          }
      ],
      "version": 30
  }
  </code></pre>
  
  <p>I used the <code>.properties</code> file to show the <code>sql</code> binding and the version that the update is doing is not the one sent in the <code>json</code>, instead of being </p>
  
  <pre><code>where id=24 and version=30, is where id=24 and version=31
  </code></pre>