是否可以在 JPA 实体内进行查询? (EclipseLink、泽西岛、杰克逊 JSON)

发布于 2024-12-08 15:30:06 字数 1141 浏览 0 评论 0原文

我有两个实体任务和任务状态。任务可以具有 TaskStatuses (1-n) 的历史记录。我在下面列出了它们(精简以消除混乱)。

@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;
}

问题是关于任务实体中的latest_status字段。我有一个提供 REST 接口的 Web 服务(基于 Jersey、Jackson 和 Tomcat)。有一个简单的 /RetrieveTask.json REST URI,它返回序列化的 Task 对象,而且效果很好。但现在我想向任务实体添加一个字段,指示任务的最新状态(可以通过按字段时间戳排序的 TaskStatus 表来确定)。最好的方法是什么?

我认为它应该是一个 Transient 字段,因为我将它存储在数据库的 Task 表中似乎没有意义。我是否应该计算 JPA 服务方法中的 Transient 字段latest_status 并使用 JP QL 查询填充该字段?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-12-15 15:30:06

实体可能有方法:

public String getLatestStatus() {
    // sort a copy of the list of task status by timestamp
    // return the status of the last element of the copy of the list
}

Entities may have methods:

public String getLatestStatus() {
    // sort a copy of the list of task status by timestamp
    // return the status of the last element of the copy of the list
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文