做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中可以吗?
谢谢。
最佳答案
不,HQL仅适用于映射的类。如果要运行SQL查询,只需使用groovy.sql.Sql。但是,如果您只想访问中间表以连接其他两个表,则这是不必要的,因为HQL已经知道表之间的关系。
关于grails - 在Grails中执行executeQuery时如何访问关系表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699934/