views:

40

answers:

3

I want a criteria that only selects bla1 and bla2 from all attributes of a given domain

Foo.findAll() or

def c = Foo.createCriteria()
def results = c {}

have:

SELECT * FROM foo

results = List of all attributes of given domain foo

want

SELECT bla1,bla2 FROM foo

written as Criteria def c = Foo.createCriteria() def results = c { ??? }

A: 

You need to use projections

def c = Foo.createCriteria()
results = c.list {
    projections {
        property('bla1')
        property('bla2')
    }
}
leebutts
+1  A: 

I think it's more natural to use HQL for this:

def results = Foo.executeQuery('SELECT bla1,bla2 FROM Foo')

The returned value will be a List of Object[], so if for example bla1 is a String and bla2 is an integer you'd access the data like

for (row in results {
   String bla1 = row[0]
   int bla2 = row[1]
   ...
}
Burt Beckwith
A: 

I am now using namedQueries which works fine:

class Item { Product product String somethingShown String somethingHidden

static belongsTo = [product:Product]
static constraints = {
    somethingShown()
    somethingHidden()
}
    static namedQueries = {
        restrictedObj { item ->
                projections {
                    property('product')
            property('somethingShown')
        }
        eq 'id', item.id
}

}

querying for restricted items

Item.restrictedObj(item).list()

Now only one question ist left, how to restrict class connected with belongsTo. For example if Product has a namedQuery "restrictedObj" too. any way to use this in criteriea property?

skurt