我无法让 Domain.count() 静态方法工作

发布于 2024-07-29 13:47:24 字数 840 浏览 2 评论 0原文

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

Description

    Counts the number of instances in the database and returns the result

Parameters

    None

Example

    def noOfBooks = Book.count()

但是,每当我调用它时,我都会收到此错误! 我简单地将对我的域类(公司)名称的调用添加到工作控制器中

 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

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

我做错了什么?

The Grails documentation defines a "count" static method, defined in the documentation like this:

Description

    Counts the number of instances in the database and returns the result

Parameters

    None

Example

    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()

and when it exectutes that line, it blows up with the following error

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

Other, working, code in the controller (pre-generated with the static scaffolding commands) access Company.get(...) etc, with no error

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2024-08-05 13:47:24

class HomeController {
    def companies = Company.count()

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

会失败,因为正如 lael 指出的那样,应用程序启动时 count() 方法不可用。 count() 方法是 GORM 添加到域类中的动态方法。 Spring(底层框架)在启动时为每个工件(控制器/服务/域类等)创建一个对象。 GORM 会在这之后。

Spring正在尝试创建一个HomeController类,HomeController的构建会在启动时将company.count的值赋给Company,但是GORM还没有启动,所以还没有添加动态方法。

无论如何,这段代码是不正确的,如果它确实有效,那么 Companies 变量将仅具有应用程序启动时公司数量的值。 一旦您“习惯”了 groovy 和 grails,我想您将会欣赏到开发的速度。

希望这可以帮助。

This

class HomeController {
    def companies = Company.count()

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

Fails because as lael pointed out the count() method is not available when the application is started. The count() method is a Dynamic method which GORM addeds to the domain classes. Spring (the underlying framework) creates an object for every artifact (Controller/Service/Domain Class etc) at start up. GORM would be after this.

Spring is trying to create a HomeController class, the construction of Home Controller will assign the value of company.count to Companies at startup, However GORM has not started yet so the dynamic methods have not been added.

This code is incorrect anyway, if it did work then the companies variable would only have the value of the number of companies at start up of the application. Once you get "used" to groovy and grails I think you will appreciate the speed of development.

Hope this helps.

夏の忆 2024-08-05 13:47:24

简短的回答是,在加载 Hibernate 插件后,count() 方法和许多其他方法会在运行时添加到元类中。 count() 不是像 Java 那样在编译时可用的静态方法,而是在运行时(显然是在解析控制器之后)添加到域的元类的静态方法。

为什么? 因为在 Hibernate/GORM 之前,使用 count()get()read() 等方法是没有意义的已初始化。 它不会连接到数据源,并且方法将无效。

我忍不住想知道为什么你需要将这样的属性放在控制器上。 一旦您保存一个新公司或删除一个公司 - 您的公司计数就会关闭。

长的答案是深入研究 Grails 的源代码,来自 GORMNamespaceHandler -> GORMSessionFactoryDe​​finitionParser -> GORMEnhancingBeanPostProcessor -> HibernatePluginSupport -> HibernatePluginSupport.addBasicPersistenceMethods()

The short answer is that the count() method and many others are added to the metaClass at runtime after the Hibernate Plugin is loaded. count() isn't a static method that is available at compile time as Java does it, but rather a static method that is added to the Domain's metaClass at runtime (apparently after parsing the controllers).

Why? Because it doesn't make sense to have count(), get() or read(), etc. methods until after Hibernate/GORM is initialized. It would not be hooked up to the datasource and the methods would be invalid.

I can't help but wonder why you would need to put a property like that on a controller. As soon as you save a new Company or delete one - your companies count would be off.

The long answer would be diving into Grails' source, From the GORMNamespaceHandler -> GORMSessionFactoryDefinitionParser -> GORMEnhancingBeanPostProcessor -> HibernatePluginSupport -> HibernatePluginSupport.addBasicPersistenceMethods()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文