【问题标题】:Left Join grails criteria左连接 grails 标准
【发布时间】:2015-07-10 09:22:23
【问题描述】:

我们有一个用户类

class User {
...
}

和另一个班级:

class Licence {

    User user
    Event event

}

我们想要做的是左连接(因为不是每个用户我们在许可类中都有一个条目)。

我们天真地这样做了:

def a = User.withCriteria {

    createAlias("Licence", "l", CriteriaSpecification.LEFT_JOIN)
    eq("id", "l.user.id")

    if (something == true)
        isNull("l.id")
    else
        isNotNull("l.id")
}

但查询失败:

could not resolve property Licence of User

有人可以帮忙查询一下吗?

【问题讨论】:

  • UserLicence 之间的连接性质是什么?是一对多还是一对一?在我看来,您需要 static hasOne = [licence: Licence]static hasMany = [licences: Licence] 用户
  • 其实是对用户的一对一。但是我没有声明 hasOne 关系,因为当 hasOne 端可以为空时,我总是遇到 hasOne 问题...

标签: grails grails-orm criteria


【解决方案1】:

问题是如果您没有外键或至少没有hasOne,grails 不知道User 端的连接。如果您有static hasOne = [licence: Licence],您的代码将按照here 的回答工作。

【讨论】:

  • 不太对。在 License 类中,有 2 个外键:user 和 event
  • 我的意思是:您需要在 User 类中有一个外键(Licence licence)或 hasOne(licence)才能使相关标准起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多