在 GORM 中创建树状结构
我试图在 GORM 中正确定义树结构,但遇到了麻烦。
我有一个域对象:
class Navigation {
Navigation parent
List children;
String name;
static belongsTo = [parent: Navigation]
static hasMany = [children: Navigation]
static constraints = {
parent(nullable: true);
}
}
和测试:
void testTree() {
Navigation root = new Navigation(name:"root");
Navigation top1 = new Navigation(name:"home");
Navigation top2 = new Navigation(name:"services");
root.addToChildren(top1).addToChildren(top2).save(flush: true);
Navigation s1 = new Navigation(name:"plumbing")
Navigation s2 = new Navigation(name:"baking")
top2.addToChildren(s1).addToChildren(s2).save(flush: true);
Navigation t = Navigation.findByName("root")
assert t.children.size() == 2
}
如果我运行此测试,我会收到此错误:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: grails.Navigation.addToChildren() is applicable for argument types: (grails.Navigation) values: [grails.Navigation : null]
如果我将该测试的内容放入 boostrap,我不会收到该错误,并且应用程序启动向上,除了导航表为空
我能做什么来解决这个问题?
I am trying to correctly define a tree structure in GORM and having trouble.
i have one domain object:
class Navigation {
Navigation parent
List children;
String name;
static belongsTo = [parent: Navigation]
static hasMany = [children: Navigation]
static constraints = {
parent(nullable: true);
}
}
and the test:
void testTree() {
Navigation root = new Navigation(name:"root");
Navigation top1 = new Navigation(name:"home");
Navigation top2 = new Navigation(name:"services");
root.addToChildren(top1).addToChildren(top2).save(flush: true);
Navigation s1 = new Navigation(name:"plumbing")
Navigation s2 = new Navigation(name:"baking")
top2.addToChildren(s1).addToChildren(s2).save(flush: true);
Navigation t = Navigation.findByName("root")
assert t.children.size() == 2
}
if i run this test, i get this error:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: grails.Navigation.addToChildren() is applicable for argument types: (grails.Navigation) values: [grails.Navigation : null]
and if i place the contents of that test into boostrap, i dont get that error, and the application starts up, except the navigation table is empty
what can i do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要模拟导航域,以便可以在单元测试中使用 GORM 方法。
将其添加到测试用例的顶部:
You need to mock the Navigation domain so that you can use the GORM methods in your unit test.
Add this to the top of your test case: