【问题标题】:How to access relationship table when doing executeQuery in Grails?在Grails中执行executeQuery时如何访问关系表?
【发布时间】:2010-04-23 15:44:47
【问题描述】:

做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中可以吗?

谢谢。

【问题讨论】:

    标签: grails hql grails-orm


    【解决方案1】:

    不,HQL 仅适用于映射类。如果要运行 SQL 查询,只需使用 groovy.sql.Sql。但是如果你只想访问中间表来连接另外两个表,那是没有必要的,因为 HQL 已经知道表之间的关系。

    【讨论】:

    • 是的,看起来是这样。我刚刚更改了架构,并且能够进行更好的查询。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    相关资源
    最近更新 更多