在Grails中执行executeQuery时如何访问关系表?
执行HQL语句时是否可以访问关系表?
例如,我有 3 个表:account、commitment、account_commitment。它是使用这些域生成的:
class Account {
static hasMany = [ commits : Commitment ]
String name
}
class Commitment {
static hasMany = [ actors : Account ]
String description
}
我的最终和实际的 SQL 查询是这样的:
SELECT
b.id,
account_name,
d.nid,
d.title
FROM
account_commitment a, // is this available in HQL?
account b,
commitment c,
content_type_act d
where
d.nid = 3332
and a.account_id = b.id
and a.act_id = c.id
and c.act_id = d.nid
我相信 HQL 只允许有效的类域。既然关系表是自动生成的,那么在HQL中可以吗?
谢谢。
Is it possible to access the relationship table when doing HQL statement?
As an example, I have 3 tables: account, commitment, account_commitment. It was generated using these domains:
class Account {
static hasMany = [ commits : Commitment ]
String name
}
class Commitment {
static hasMany = [ actors : Account ]
String description
}
My final and actual SQL query is something like this:
SELECT
b.id,
account_name,
d.nid,
d.title
FROM
account_commitment a, // is this available in HQL?
account b,
commitment c,
content_type_act d
where
d.nid = 3332
and a.account_id = b.id
and a.act_id = c.id
and c.act_id = d.nid
I believe HQL only allows valid class domains. Since the relationship table is automatically generated, is this possible in HQL?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,HQL 仅适用于映射类。如果你想运行 SQL 查询,只需使用 groovy.sql.Sql。但如果您只想访问中间表来连接另外两个表,则没有必要,因为 HQL 已经知道表之间的关系。
No, HQL only works with mapped classes. If you want to run SQL queries just use groovy.sql.Sql. But if you only want to access the intermediate table to join the other two, that's unnecessary since HQL knows about the relationships between tables already.