In a Spring Boot Applicaion, I have an entity Task with a status that changes during execution:
@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); } } If state changes, the object should be saved in the database. I'm using this Spring Data interface as repository:
public interface TaskRepository extends CrudRepository<Task,Long> {} And this code to create and start a Task:
Task t1 = new Task("Task 1"); Task persisted = taskRepository.save(t1); persisted.start(); From my understanding persisted 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.
Any ideas what I'm doing wrong here?