gpt4 book ai didi

grails - Grails标准投影-方法projections()的签名不适用于参数类型

转载 作者:行者123 更新时间:2023-12-02 15:25:41 25 4
gpt4 key购买 nike

根据Grails文档

Grails also lets you write your domain model in Java or reuse an existing one that already has Hibernate mapping files. Simply place the mapping files into grails-app/conf/hibernate and either put the Java files in src/java or the classes in the project's lib directory if the domain model is packaged as a JAR. You still need the hibernate.cfg.xml though!



所以这正是我所做的。

我使用了Java域模型和hibernate.cfg.xml文件进行映射。我也用
{DomainName} Constraints.groovy用于添加Grails约束。我还曾经向{DomainName} Constraints添加函数。例如,以下是我的EmployeeConstraints.groovy的内容
Employee.metaClass.static.findByDepartment = {depCode ->
createCriteria().list {
department{
inList ('code', depCode)
}
}
}

现在可以正常工作了。但是,当我向其添加投影(下面的代码)时,只是为了获取员工代码。
Employee.metaClass.static.findByDepartment = {depCode ->
createCriteria().list {
projections { property('empCode', 'empCode') }
department { inList ('code', depCode) }

}

}

我收到以下错误。

“没有方法签名:com.package.script142113.projections()适用于参数类型。”

有人可以指出代码的错误吗?

谢谢!

最佳答案

property投影用于返回对象属性的子集。例如,仅返回foobar属性,请使用:

projections { 
property('foo')
property('bar')
}

之所以出错,是因为您使用2个参数而不是1个参数调用了 property方法。

顺便说一下,我发现您的代码还有另一个潜力。 Grails将自动创建一个动态查找程序 findByDepartment,该名称与您尝试通过元类添加的方法同名。我不知道哪个会占优,但我建议您通过使用Grails的 named query支持添加此查询,并使用类似 getByDepartment的名称来避免该潜在问题并简化代码,以使名称不会与动态查找器。

关于grails - Grails标准投影-方法projections()的签名不适用于参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27919547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com