gpt4 book ai didi

grails - 我无法使用Domain.count()静态方法来工作

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

Grails文档定义了一个“count”静态方法,该方法在文档中定义如下:

Description    Counts the number of instances in the database and returns the resultParameters    NoneExample    def noOfBooks = Book.count()

However, whenever I call it, I get this error! I simple added a call to the name of my Domain Class (Company) like this to a working Controller

 def companies = Company.count()

当它拔出那条线时,它会爆炸并显示以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass

Controller 中的其他有效代码(使用静态脚手架命令预先生成)访问Company.get(...)等,没有错误

我究竟做错了什么?

最佳答案

这个

class HomeController {
def companies = Company.count()

def index = {
render(view:"index")
}
}

失败,因为正如lael指出的那样,启动应用程序时count()方法不可用。 count()方法是一种动态方法,GORM将其添加到域类中。 Spring(底层框架)在启动时为每个 Artifact (Controller / Service / Domain Class等)创建一个对象。 GORM将在此之后。

Spring正在尝试创建HomeController类,Home Controller的构造将在启动时将company.count的值分配给Companies,但是GORM尚未启动,因此未添加动态方法。

该代码无论如何都是不正确的,如果它确实起作用了,那么company变量将仅具有应用程序启动时的公司数量值。一旦您“习惯”了常规和粗俗的用法,我想您将欣赏开发的速度。

希望这可以帮助。

关于grails - 我无法使用Domain.count()静态方法来工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1228673/

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