如何使用 Hibernate Criteria 通过 OneToMany 关系连接两个表
我有两个表,除非我有循环依赖,否则我无法在其实体中实现映射 @OneToMany
。我想创建一个 Hibernate Criteria,以便我能够在 ids 字段上加入此表并加上限制。然而,我有一个 sql 查询,它给了我一个我正在寻找的结果,我无法理解如何实现这些条件。
具有共享 ID 的表:
| Table A | Table B |
|id | id |
|languageId | code |
|comment | |
SQL 查询:
选择a.id、a.languageId、a.comment、 b.code from TableA a join TableB b on a.id=b.id
有人可以帮我写 Hibernate Criteria 吗?
先感谢您, L。
I have two tables for which I can't implement mapping @OneToMany
in their entities unless I have a circle dependency. I want to create a Hibernate Criteria so that I'd be able to join this tables on ids' fields plus a restriction. However, I have a sql query which gives me a result I'm looking for, I failed to understand how to implement the criteria.
Tables which have a shared id:
| Table A | Table B |
|id | id |
|languageId | code |
|comment | |
SQL query:
select a.id, a.languageId, a.comment,
b.code from TableA a join TableB b on
a.id=b.id
Could anybody help me to write the Hibernate Criteria?
Thank you in advance,
L.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能这样做 - Hibernate Criteria 不支持任意条件下的连接。
您必须使用 HQL 查询(它也不支持任意条件的
JOIN
语法,但您可以使用旧式语法):You cannot do it - Hibernate Criteria doesn't support join on arbitrary conditions.
You have to use HQL query (it doesn't support
JOIN
syntax with arbitrary condition too, but you can use old-style syntax):