在 Hibernate 中从一对多关系中选择一个对象

发布于 2024-09-04 17:57:02 字数 428 浏览 2 评论 0原文

我有两个表:

Job
job_id, <other data>

Job_Link
job_link_id, job_id, start_timestamp, end_timestamp, <other data>

Job_Link 中可能有多个记录指定相同的 job_id 以及 start_timestamp 和 end_timestamps 来指示这些记录何时被视为“当前”,保证 start_timestamp 和 end_timestamp 不会重叠。

我们有 Job 和 Job_Link 表的实体,定义一对多关系来加载所有 job_link 不会有问题。然而,我们希望避免这种情况,并在 Job 实体中拥有一个仅包含“当前”Job_Link 对象的 job_link 项。

有什么办法可以实现这一点吗?

I have two tables:

Job
job_id, <other data>

Job_Link
job_link_id, job_id, start_timestamp, end_timestamp, <other data>

There may be multiple records in Job_Link specifying the same job_id with the start_timestamp and end_timestamps to indicate when those records are considered "current", it is guaranteed that start_timestamp and end_timestamp will not overlap.

We have entities for both the Job and Job_Link tables and defining a one-to-many relationship to load all the job_links wouldn't be a problem. However we'd like to avoid that and have a single job_link item in the Job entity that will contain only the "current" Job_Link object.

Is there any way to achive that?

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

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

发布评论

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

评论(1

樱&纷飞 2024-09-11 17:57:02

使用 @WhereJoinTable 注释是可能的。在 Job 实体中添加 @OneToOne 链接,就像在 JobLink 对象上定义 @OneToMany 一样。

@OneToOne(...)
@WhereJoinTable(clause = "now() between start_timestamp and end_timestamp") // mysql
@WhereJoinTable(clause = "sysdate between start_timestamp and end_timestamp") // oracle ...
private JobLink jobLink

缺点:它破坏了数据库和 JPA 提供程序的可移植性

It is possible with the @WhereJoinTable annotation. In the Job entity add a @OneToOne link exactly as you would define the @OneToMany on the JobLink object.

@OneToOne(...)
@WhereJoinTable(clause = "now() between start_timestamp and end_timestamp") // mysql
@WhereJoinTable(clause = "sysdate between start_timestamp and end_timestamp") // oracle ...
private JobLink jobLink

Drawbacks : it breaks the database and JPA provider portability

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文