title: Why do changes to my JPA entity not get persisted to the database?
body: <p>In a Spring Boot Applicaion, I have an entity <code>Task</code> with a status that changes during execution:</p>
  
  <pre><code>@Entity
  public class Task {
  
    public enum State {
      PENDING,
      RUNNING,
      DONE
    }
  
    @Id @GeneratedValue
    private long id;
    private String name;
    private State state = State.PENDING;
  
    // Setters omitted
  
    public void setState(State state) {
      this.state = state; // THIS SHOULD BE WRITTEN TO THE DATABASE
    }
  
    public void start() { 
      this.setState(State.RUNNING);
  
      // do useful stuff
      try { Thread.sleep(2000); } catch(InterruptedException e) {}
      this.setState(State.DONE);
    }
  } 
  </code></pre>
  
  <p>If state changes, the object should be saved in the database. I'm using this Spring Data interface as repository:</p>
  
  <pre><code> public interface TaskRepository extends CrudRepository&lt;Task,Long&gt; {}
  </code></pre>
  
  <p>And this code to create and start a <code>Task</code>:</p>
  
  <pre><code>Task t1 = new Task("Task 1");
  Task persisted = taskRepository.save(t1);
  persisted.start();
  </code></pre>
  
  <p>From my understanding <code>persisted</code> is now attached to a persistence session and if the object changes this changes should be stored in the database. But this is not happening, when reloading it the state is PENDING.</p>
  
  <p>Any ideas what I'm doing wrong here?</p>