[英]Grails Domain With “First” and “Last” Properties
我最近继承了Grails代码库,其中包含名为Name
的域类(以及其他),属性first
和last
分别代表名称的第一个和最后一个部分。 在编写使用该域的单元测试时,我遇到了一些问题,这些问题源于这些属性的名称与Grails中的第一个和最后一个方法相同。 现在,我可以通过重命名的属性来解决这些问题,但我想知道是否有内Grails的一种方法,使用该属性的名称first
和last
。
即,我收到的错误是No signature of method: com.example.Name.first() is applicable for argument types: () values: [] Possible solutions: first(), first(java.lang.String), first(java.util.Map), list(), list(java.util.Map), print(java.lang.Object)
当Grails尝试对属性应用nullable: true
为nullable: true
约束时No signature of method: com.example.Name.first() is applicable for argument types: () values: [] Possible solutions: first(), first(java.lang.String), first(java.util.Map), list(), list(java.util.Map), print(java.lang.Object)
。
这是Name
的来源:
class Name {
String first
String middle
String last
static belongsTo = [person : Person]
static constraints = {
first(nullable:true)
middle(nullable:true)
last(nullable:true)
}
public static Name findOrCreate(String first, String middle, String last){
def name
name = Name.createCriteria().get{
and{
eq('first', first)
eq('middle', middle)
eq('last', last)
}
if(!name){
name = new Name()
name.first = first
name.middle = middle
name.last = last
}
return name
}
static mapping = {
cache true
}
}
你说这个错误发生在约束块中。 在这种情况下,您可以使它与显式delegate.
,即
static constraints = {
delegate.first(nullable:true)
// and similarly for last
}
强制第first
被视为对约束DSL的呼叫,而不是对静态GORM方法的呼叫。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.