【问题标题】:How can I make use of the class annotated with @grails.persistence.Entity in the jar如何使用 jar 中带有 @grails.persistence.Entity 注释的类
【发布时间】:2013-04-11 06:30:11
【问题描述】:

在我的 grails 项目中,我想将公共 jar 文件中的域类与 @grails.persistence.Entity 注释一起使用。但是当我按照doc from grails site中的步骤进行操作时

我添加了喜欢的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration SYSTEM
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping package="com.books" />
        <mapping class="com.books.Book" />
    </session-factory>
</hibernate-configuration>

到 grails-app/conf/hibernate/ 目录。 但是当我启动项目时,它会报告错误日志:

2013-04-11 14:24:53,928 - org.codehaus.groovy.grails.web.context.GrailsContextLoader -20516 [pool-5-thread-1] ERROR  - Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: static com.baoxian.task.inscar.entity.QuoteTask.findById() is applicable for argument types: (java.lang.Long) values: [2851]
Possible solutions: find(), findAll(), findAll(groovy.lang.Closure), find(groovy.lang.Closure)
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static com.baoxian.task.inscar.entity.QuoteTask.findById() is applicable for argument types: (java.lang.Long) values: [2851]
Possible solutions: find(), findAll(), findAll(groovy.lang.Closure), find(groovy.lang.Closure)
    at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:300)
    at grails.util.Environment.executeForEnvironment(Environment.java:293)
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:269)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: groovy.lang.MissingMethodException: No signature of method: static com.baoxian.task.inscar.entity.QuoteTask.findById() is applicable for argument types: (java.lang.Long) values: [2851]
Possible solutions: find(), findAll(), findAll(groovy.lang.Closure), find(groovy.lang.Closure)
    at BootStrap$_closure1.doCall(BootStrap.groovy:22)
    ... 8 more
Disconnected from the target VM, address: '127.0.0.1:63383', transport: 'socket'

Process finished with exit code 1

很明显@Entity 不起作用。 此外,我的 BuildConfig.groovy 喜欢:

compile ('com.baoxian:baoxian-task-inscar-manager:1.2.5-SNAPSHOT')
                {
                    excludes "org.grails", "org.slf4j", "xml-apis","mysql","org.hibernate","hsqldb","hibernate-annotations","org.quartz-scheduler","org.springframework"
                }

com.baoxian:baoxian-task-inscar-manager:1.2.5-SNAPSHOT 就是我的域类来自的地方

这是我第一次在 stackoverflow 上提问。 ^_^

【问题讨论】:

  • 你把hibernate.cfg.xml放在哪里?

标签: grails grails-domain-class


【解决方案1】:

如果我错过了更简单/更好的问题解决方案,请有人纠正我。

@yidao620c,我怕你做的不行……

我推荐 2 个解决方案(对于您似乎想要做的事情,我通常选择第二个选项):

1) JPA 实体注解不是 Grails 实体注解

如果您想遵循文档,我相信您的 JPA 类必须驻留在 Grails 项目中(JPA 注释,而不是 grails @Entity 注释)。

对您来说最简单的路径可能是 JPA 插件 http://grails.org/JPA+Plugin。如果您仍需要动态查找器,请跳至选项 2)。

2) Gorm ouside Grails 以注入动态查找器

如果您想从外部 jar(非 Grails 项目)导入带有 grails 注释的实体,您需要一些额外的步骤来注入动态查找器(findAll,等等)。

由于这可能是一篇包含所有细节的长篇文章,我将很快制作一个关于如何做到这一点的视频(提示:http://www.grails.org/GORM+-+StandAlone+Gorm)。

但是,这里有一个简短的回答:

  • 创建一个 Groovy 项目(我通常使用 Gradle 作为构建系统)。
  • 依赖 Grails Gorm、Grails bootstrap 和少数 其他(slf4j、Spring 等)
  • 有一个 Spring 配置(XML 或 Grails Spring DSL)来注入一个 Gorm 会话工厂。您可以使用简单的主类快速测试动态查找器 -> 加载 Spring Beans 以设置 Gorm -> 使用 withNewSessionwithTransaction 或使用服务调用域对象。
  • 当您构建 jar 时,编译阶段将增强您的实体以注入动态 Gorm 方法。

【讨论】:

  • 在我的 grails 项目中,我想使用 jar 文件中的类。它由实体注释。但你建议在 Grails 之外。不是我的需要。无论如何,非常感谢
猜你喜欢
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多