在 Hibernate 中从一对多关系中选择一个对象
我有两个表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 @WhereJoinTable 注释是可能的。在 Job 实体中添加 @OneToOne 链接,就像在 JobLink 对象上定义 @OneToMany 一样。
缺点:它破坏了数据库和 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.
Drawbacks : it breaks the database and JPA provider portability