Is there a 'not in' equivalent in GORM?

前端 未结 5 937
我寻月下人不归
我寻月下人不归 2021-02-15 10:21

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


        
相关标签:
5条回答
  • 2021-02-15 10:54

    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.

    0 讨论(0)
  • 2021-02-15 10:58

    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)
                    }
                }
            }
    
    0 讨论(0)
  • 2021-02-15 10:59

    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)

    0 讨论(0)
  • 2021-02-15 11:10

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

    not { inList 'age',[18..65] }
    
    0 讨论(0)
  • 2021-02-15 11:11

    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!

    0 讨论(0)
提交回复
热议问题