I have two entities Task and TaskStatus. A task can have a history of TaskStatuses (1-n). I listed them below (stripped down to remove clutter).
@Entity
public class Task implements Serializable {
@Id
private int task_id;
private String name;
private String description;
@JsonManagedReference("task-taskstatuses")
@OneToMany(fetch = FetchType.LAZY, mappedBy="task")
private Collection<TaskStatus> task statuses;
@Transient private String latest_status; // this field I want to calculate based on the task statuses
}
@Entity
@IdClass(TaskStatusId.class)
public class TaskStatus implements Serializable {
@Id
@ManyToOne
@JoinColumn(name="task_id", referencedColumnName="task_id")
private Task task;
@Id
private Timestamp timestamp;
private String status;
private String username;
}
The question is about the field latest_status in the Task entity. I have a web service that offers a REST interface (based on Jersey, Jackson and Tomcat). There is a simple /RetrieveTask.json REST URI that returns a serialized Task object, and that works perfectly. But now I wanted to add a field to the Task entity that indicates the latest status of the Task (which can be determined by the TaskStatus table sorted on the field timestamp). What is the best way to do that?
I though it should be a Transient field, because I did not seem to make sense to store it in the Task table in the database. Should I calculate that Transient field latest_status in the JPA service method and fill the field in there using a JP QL query?
Entities may have methods: