【问题标题】:Grails domain class method from java source error: cannot find symbol来自java源错误的Grails域类方法:找不到符号
【发布时间】:2016-04-24 14:18:16
【问题描述】:

在我的应用程序中,我有一个 grails 域类 Student。我在 src.java 中也有一些类,最初是用纯 java 编写的。

在其中一个课程中,我试图以某种方式收集学生的集合:

this.students = new ArrayList<Student>(Student.findAll());

Grails 包已导入,intellij 没有报错,但是我在尝试编译时遇到了愚蠢的错误。

[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  [groovyc] Compile error during compilation with javac.
  [groovyc] F:\path\ProjectName\src\java\grb\StudentSchedule.java:58: error: cannot find symbol
  [groovyc]         this.students = new ArrayList<Student>(Student.findAll());
  [groovyc]                                                       ^
  [groovyc]   symbol:   method findAll()
  [groovyc]   location: class Student

我也一直在尝试不同的方法,在 Student 上,但都给我同样的错误 - 找不到符号。

【问题讨论】:

  • 方法findAll() 由Grails 动态添加到所有域类中。所以调用方法依赖于 Groovy 的动态调度来找到方法然后执行。因为类本身不存在该方法,所以不能直接从Java中调用。
  • 伊曼纽尔,感谢您的评论!有什么办法可以解决吗?
  • 用 groovy 而不是 Java 编写这些类
  • 您使用的是哪个版本的 Grails?

标签: java grails groovy


【解决方案1】:

Grails docs 状态:您也可以用 Java 编写 Grails 域类
您必须使用 Java 中的 org.codehaus.groovy.runtime.InvokerHelper
欲了解更多信息,请参阅mailing list
示例代码的种类:

import my.package.User
import org.codehaus.groovy.runtime.InvokerHelper;

List allInstances = (List)InvokerHelper.invokeMethod(User.class, "list", null)); 

User one=(User)InvokerHelper.invokeMethod(User.class, "get", id);

也许this post 也可以提供帮助。
但我同意用 Groovy 编写类的想法。

【讨论】:

  • 谢谢迈克利斯!这就是我最终做的事情,而且奏效了!
猜你喜欢
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多