【问题标题】:how to left join with 2 table with a condition in grails如何在 grails 中有条件的 2 个表左连接
【发布时间】:2016-01-06 09:32:09
【问题描述】:

使用 grails 2.1.1,我需要使用左连接构建一个包含 2 个表的查询。如果我在 oracle 中查询它,它正在工作并给我正确的结果。当我使用 grails 控制器时,出现错误。谁能帮我解决这个问题?

以下是我的尝试:

我在 oracle 上的查询:

SELECT  MS.* FROM 
   SLS_DO_MST MS  LEFT OUTER JOIN INV_ISSUE ISS 
   ON MS.MID = ISS.SLS_DO_MST_MID 
   where ISS.SLS_DO_MST_MID is null

我在 grails 控制器中使用的 hql 查询:

def items = SlsDoMst.executeQuery('select a from sls.dlo.SlsDoMst a left outer join  inv.InvIssue b on a.id = b.slsDoMst.id where b.slsDoMst.id is null')

我得到的错误:

unexpected token: on near line 1, column 66 [select a from sls.dlo.SlsDoMst a left outer join  inv.InvIssue b on a.id = b.slsDoMst.id where b.slsDoMst.id is null]

我的域名如下::

我的 SlsDoMst 域 >>>

    class SlsDoMst {

    ...

    static mapping = {
        ...
    }

    static constraints = {
        ...
    }
}

我的 InvIssue 域 >>>

    class InvIssue{
    static mapping = {
        table 'INV_ISSUE'
        slsDoMst column: 'SLS_DO_MST_MID',ignoreNotFound: true
        ...
    }

    ...
    SlsDoMst slsDoMst

    static constraints = {
    ...
    }
}

【问题讨论】:

    标签: hibernate grails grails-orm grails-2.0 hibernate-criteria


    【解决方案1】:

    您不能在 HQL 中使用 on 进行左连接。 Hibernate 由模型定义决定将哪一列用于连接子句。

    您需要在 HQL 上加入所使用的模型上定义关系(例如 hasMany)和 mapping

    这里是参考: https://grails.github.io/grails-doc/2.4.3/ref/Database%20Mapping/joinTable.html

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2015-01-12
    • 2023-04-08
    • 1970-01-01
    • 2011-06-30
    • 2018-05-22
    • 2013-11-09
    相关资源
    最近更新 更多