Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 从foo中选择bla1、bla2的标准_Hibernate_Grails_Criteria - Fatal编程技术网

Hibernate 从foo中选择bla1、bla2的标准

Hibernate 从foo中选择bla1、bla2的标准,hibernate,grails,criteria,Hibernate,Grails,Criteria,我想要一个仅从给定域的所有属性中选择bla1和bla2的条件 Foo.findAll()或 def c = Foo.createCriteria() def results = c {} 拥有: 结果=给定域foo的所有属性列表 想要 作为标准编写 def c=Foo.createCriteria() def结果=c{ ??? }您需要使用投影 def c = Foo.createCriteria() results = c.list { projections { p

我想要一个仅从给定域的所有属性中选择bla1和bla2的条件

Foo.findAll()

def c = Foo.createCriteria()
def results = c {}
拥有:

结果=给定域foo的所有属性列表

想要

作为标准编写 def c=Foo.createCriteria() def结果=c{ ???
}您需要使用投影

def c = Foo.createCriteria()
results = c.list {
    projections {
        property('bla1')
        property('bla2')
    }
}

我认为使用HQL更自然:

def results = Foo.executeQuery('SELECT bla1,bla2 FROM Foo')
返回的值将是对象[]的列表,因此,例如,如果bla1是字符串,bla2是整数,则可以访问如下数据

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

我现在使用的namedQueries工作正常:

类项目{ 产品 如图所示 把什么东西藏起来

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

查询受限项目

Item.restrictedObj(item).list()

现在只剩下一个问题,如何限制与belongsTo相关的类。例如,如果产品也有一个名为“restrictedObj”的查询。有没有办法在Criteria属性中使用它?

那么你的问题是?似乎你已经有了awnser?ups我的错,编辑上面,谢谢你的域名真的有那么多属性,你需要担心尝试这样优化吗?如果是这样,那么您可能需要稍微分解一下对象模型。
static belongsTo = [product:Product]
static constraints = {
    somethingShown()
    somethingHidden()
}
    static namedQueries = {
        restrictedObj { item ->
                projections {
                    property('product')
            property('somethingShown')
        }
        eq 'id', item.id
}
Item.restrictedObj(item).list()