有没有一个“不在”? GORM 中的等效项?

发布于 2024-09-13 12:06:55 字数 408 浏览 6 评论 0原文

这可以在 createCriteria() 中转换吗?

SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10

我知道有一个“in”运算符,这就是我到目前为止所拥有的:

def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        eq('type', 'act')
    }
    maxResults(10)
}

只是想看看这是否可能。否则,我想这在 HQL 中是可能的,对吗?

Is this possible to convert in createCriteria()?

SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10

I know there's a 'in' operator and here's what I have so far:

def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        eq('type', 'act')
    }
    maxResults(10)
}

Just want to see if this is possible. Otherwise, I guess this is possible in HQL right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

因为看清所以看轻 2024-09-20 12:06:55

感谢 Sammyrulez 提供的代码。从中得到了一个想法。测试了一下,但没有用。我修复了它,这是最终的工作代码:

def ids = [14400 as long, 14401 as long]

def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        eq('type', 'act')
        not { 'in'(ids) }
    }
    maxResults(10)
}

现在我知道如何使用“not”运算符。多谢!

thanks Sammyrulez for the code. got an idea from that. tested it but it didn't work. i fixed it and here's the final working code:

def ids = [14400 as long, 14401 as long]

def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        eq('type', 'act')
        not { 'in'(ids) }
    }
    maxResults(10)
}

now i know how to use 'not' operator. thanks a lot!

み零 2024-09-20 12:06:55

我自己没有尝试过,但是查看 Grails 文档和 hibernate api,您可以使用 Hibernate Criteria API 的 Restrictions 类中找到的静态方法在此构建器映射上创建节点 1。所以就像

 def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        not(in('propertyName', ['val1','val2']))
    }
    maxResults(10)
}

既然你将 in 方法(返回一个 Criterion)与 not 方法(将 Criterion 作为参数并返回一个否定版本)链接起来

not tried it myself but looking at the Grails doc and hibernate api you create nodes on this builder map with the static methods found in the Restrictions class of the Hibernate Criteria API 1. So something like

 def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        not(in('propertyName', ['val1','val2']))
    }
    maxResults(10)
}

Since you chain the in method (that returns a Criterion) with the not method (that takes a Criterion as argument and returns a negated version)

最美不过初阳 2024-09-20 12:06:55

这是解决方案:

def resultat=EnteteImputationBudgetaire.createCriteria().get{
            between("dateDebutPeriode", dateDebut, dateFin)

            and{ eq 'natureImputationBudgetaire','FONCTIONNEMENT'  }
            maxResults(1)
        }

        def resultat2=ParametragePlanBudgetaire.createCriteria().list() {
            like('composantBudgetaire','6%')
            if(resultat?.details) {
                not {
                    'in'('composantBudgetaire',resultat?.details?.imputationBudgetaire)
                }
            }
        }

this is the solution :

def resultat=EnteteImputationBudgetaire.createCriteria().get{
            between("dateDebutPeriode", dateDebut, dateFin)

            and{ eq 'natureImputationBudgetaire','FONCTIONNEMENT'  }
            maxResults(1)
        }

        def resultat2=ParametragePlanBudgetaire.createCriteria().list() {
            like('composantBudgetaire','6%')
            if(resultat?.details) {
                not {
                    'in'('composantBudgetaire',resultat?.details?.imputationBudgetaire)
                }
            }
        }
月下伊人醉 2024-09-20 12:06:55

根据有关创建条件的 Grails 文档此处,您可以使用类似这样的内容:

not {'in'("age",[18..65])}

在此示例中,您有一个名为 "age" 的属性,并且您想要获取不在 18 到 65 之间的行。当然,[18..65 ] 部分可以替换为您需要的任何值列表或范围。

According to Grails documentation about creating criteria here, you can use something like this:

not {'in'("age",[18..65])}

In this example, you have a property named "age" and you want to get rows that are NOT between 18 and 65. Of course, the [18..65] part can be substituted with any list of values or range you need.

趁微风不噪 2024-09-20 12:06:55

请记住:在这种情况下,您不必使用括号,您可以使用 inList,例如:

not { inList 'age',[18..65] }

Just remembering: in this case you don't have to use parenthesis and you can use inList, for example:

not { inList 'age',[18..65] }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文