我有兩個領域類這樣的,第一即Manager
:似乎Groovy在這兩種情況下行爲不同?
package com.mnm
class Manager {
String name;
static hasMany = [ project : Project, tasks : Tasks ]
static constraints = {
}
}
而且即第二個,Project
:
package com.mnm
class Project {
String projectTitle
String projectDescription
String description
static belongsTo = [ managers: Manager ]
static hasMany = [ tasks : Tasks ]
static constraints = {
}
}
我寫集成測試,像這樣(尋找projects
名通過使用Manager
):
void testCountProject() {
def manager = new Manager(name:'Anto').save()
manager.addToProject(new Project(projectTitle:'Grails'))
manager.addToProject(new Project(projectTitle:'Griffon'))
def noOfProjects = Manager.get(manager.id)
def found = noOfProjects.project.collect { it.projectTitle }
assertEquals(['Grails','Griffon'], found.sort())
}
那麼沒有錯誤在它和測試通過!但是,當我添加更多的東西到像相同的測試(現在我想反,通過使用Project
找到Manager
名):
void testCountProject() {
def manager = new Manager(name:'Anto').save()
def project1 = new Project(projectTitle:'Grails').save()
manager.addToProject(project1)
manager.addToProject(new Project(projectTitle:'Griffon'))
def noOfProjects = Manager.get(manager.id)
def found = noOfProjects.project.collect { it.projectTitle }
assertEquals(['Grails','Griffon'], found.sort())
def noOfManager = Project.get(project.id)
def foundManager = noOfManager.managers.collect { it.name }
assertEquals(['Anto'],foundManager)
}
現在我得到的錯誤是這樣的:
No signature of method: com.mnm.Manager.addToProject() is applicable for argument types: (null) values: [null] Possible solutions: addToProject(java.lang.Object), getProject()
我哪裏出錯了?
在此先感謝。
你能更多地解釋你的意思通過「,默認情況下所有的屬性都不是null,所以當你只設置了」projectTitle「時,你的Project實例就無法通過驗證。 ? –
驗證?但是在我的領域類中,我根本沒有做過任何類型的驗證:/那麼驗證失敗如何? –
默認情況下,所有屬性都是必需的。你可以用一個約束'nullable:true'來改變它。所以如果你創建一個新的'Project'並且只設置'projectTitle',你會得到'projectDescription'和'description'的驗證錯誤。調用'save()'首先調用'validate()'以確保不會將無效數據推送到數據庫。你可以明確地調用'validate()',但它也是爲你完成的。 –