在Grails3.x中使用JPA注释进行映射

在Grails3.x中使用JPA注释进行映射,grails,hibernate-mapping,grails-domain-class,Grails,Hibernate Mapping,Grails Domain Class,我在测试应用程序中使用Grails中的JPA注释类(在Grails-3.1.11和Grails-3.2.0上尝试过)时遇到问题 我按照Grails文档中的描述和Stackoverflow中类似问题的多个答案(、和),但运气不好 以下是我所做的描述(在两个版本的Grails上相同): 1.创建一个新的Grails项目(我正在使用IntelliJ Idea 2016.2.4)。 2.在应用程序.yml中设置数据源: dataSource: configClass: org.codehaus.

我在测试应用程序中使用Grails中的JPA注释类(在Grails-3.1.11和Grails-3.2.0上尝试过)时遇到问题

我按照Grails文档中的描述和Stackoverflow中类似问题的多个答案(、和),但运气不好

以下是我所做的描述(在两个版本的Grails上相同):
1.创建一个新的Grails项目(我正在使用IntelliJ Idea 2016.2.4)。
2.在应用程序.yml中设置数据源:

dataSource:
    configClass: org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.class
    pooled: true
    jmxExport: true
    dialect: org.hibernate.dialect.PostgreSQLDialect
    driverClassName: org.postgresql.Driver
    username: postgres
    password: masterkey
compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1200-jdbc41'
三,。将PostgreSQL JDBC依赖项添加到build.grandle

dataSource:
    configClass: org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.class
    pooled: true
    jmxExport: true
    dialect: org.hibernate.dialect.PostgreSQLDialect
    driverClassName: org.postgresql.Driver
    username: postgres
    password: masterkey
compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1200-jdbc41'
四,。在src/main/groovy中创建新类DictionaryEntity.groovy

五,。在grails app/conf目录中创建hibernate.cfg.xml(也尝试了grails app/conf/hibernate):

当我运行应用程序时,没有域类(artifacts中的“域:0”)。如果我转到我的控制器(“/dictionaryEntity/index”),我会得到以下错误:

    ERROR org.grails.web.errors.GrailsExceptionResolver - MissingMethodException occurred when processing request: [GET] /dictionaryEntity/index
    No signature of method: static persistence.postgresql.mapping.DictionaryEntity.count() is applicable for argument types: () values: []
    Possible solutions: print(java.lang.Object), print(java.io.PrintWriter), wait(), find(), collect(), any(). Stacktrace follows:
    java.lang.reflect.InvocationTargetException: null
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:210)
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187)
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: groovy.lang.MissingMethodException: No signature of method: static persistence.postgresql.mapping.DictionaryEntity.count() is applicable for argument types: () values: []
    Possible solutions: print(java.lang.Object), print(java.io.PrintWriter), wait(), find(), collect(), any()
    at grails.rest.RestfulController.countResources(RestfulController.groovy:277)
    at grails.rest.RestfulController.index(RestfulController.groovy:64)
    at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
    ... 14 common frames omitted
看起来DictionaryEntity尚未识别为域类。 我的纯Java+Hibernate应用程序可以很好地使用相同的映射类,因此我需要您的建议来解决这个问题

更新
添加注释@grails.gorm.Entity关于Joshua的答案,错误已更改为:

Either class [persistence.postgresql.mapping.DictionaryEntity] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.

GORM 5似乎不支持带有hibernate.cfg.xml的JPA。查看此pull request对话了解更多详细信息。grails主文档似乎不正确,需要更新


也可以考虑@ Gray.Gr.St实体。如果您控制源

是,您的答案确实存在,问题仍然存在,但是错误消息已经更改为<代码>。在调用GORM实体上的任何方法之前,确保正确加载和配置GORM。
Either class [persistence.postgresql.mapping.DictionaryEntity] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.