当前位置: 首页 > 知识库问答 >
问题:

在Grails 3中使用JPA注释进行映射。x

陈翰林
2023-03-14

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

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

3.将PostgreSQL JDBC依赖添加到build.grandle:

compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1200-jdbc41'

4、创建新的类DictionaryEntity。src/main/groovy中的groovy:

package persistence.postgresql.mapping

import javax.persistence.*;

@Entity(name = "persistence.postgresql.mapping.DictionaryEntity")
@Table(name = "dictionary")
public class DictionaryEntity implements Serializable {
    private int id;
    private int word;
    private int language;
    private String txt;

    @SequenceGenerator(name = "dictionary_id_seq_gen", sequenceName = "dictionary_id_seq", allocationSize = 1)
    @Id
    @GeneratedValue(generator = "dictionary_id_seq_gen", strategy = GenerationType.SEQUENCE)
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "word", nullable = false)
    public int getWord() {
        return word;
    }

    public void setWord(int word) {
        this.word = word;
    }

    @Column(name = "language", nullable = false)
    public int getLanguage() {
        return language;
    }

    public void setLanguage(int language) {
        this.language = language;
    }

    @Column(name = "txt", length = 128)
    public String getTxt() {
        return txt;
    }

    public void setTxt(String txt) {
        this.txt = txt;
    }

    public DictionaryEntity() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DictionaryEntity that = (DictionaryEntity) o;

        if (id != that.id) return false;
        if (word != that.word) return false;
        if (language != that.language) return false;
        if (txt != null ? !txt.equals(that.txt) : that.txt != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + word;
        result = 31 * result + language;
        result = 31 * result + (txt != null ? txt.hashCode() : 0);
        return result;
    }
}

5、创建Hibernate。cfg。grails app/conf目录中的xml(也尝试了grails app/conf/hibernate):

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="persistence.postgresql.mapping.DictionaryEntity"/>
    </session-factory>
</hibernate-configuration>

6.在grails-app/控制器/grailsproj目录中创建了一个新的DictionaryEntityController(脚手架插件已包含到依赖项中):

package grailsproj

import persistence.postgresql.mapping.DictionaryEntity

class DictionaryEntityController {

    static scaffold = DictionaryEntity
}

当我运行应用程序时,没有域类(“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.实体关于约书亚的答案,错误已更改为:

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.

共有1个答案

寇甫
2023-03-14

GORM 5似乎不支持JPAhibernate.cfg.xml.查看此拉取请求对话以获取更多详细信息。主grails文档似乎不正确,需要更新https://github.com/grails/grails-data-mapping/pull/678

如果您控制源,还可以考虑@grails.gorm.实体

 类似资料:
  • 如何在Grails 3.0.1中映射带有注释的域类? 以下步骤对我不起作用。 步骤1.我使用Grails 3.0.1创建了一个新应用程序()。 步骤2。如Hibernate注释映射中所述,我在src/main/com/books/Book中创建了一个新类。groovy(也是groovy) 步骤3。然后通过将相关条目添加到grails-app/conf/Hibernate/Hibernate,向Hi

  • 问题内容: 如何在Grails 3.0.1中使用注释映射域类? 以下步骤对我不起作用。 步骤1 。我使用Grails 3.0.1()创建了一个新应用程序。 第二步 。如“ 使用hibernate注释映射”中所述,我在(也尝试过)中创建了一个新类。 第三步 。然后通过向文件中添加相关条目,向Hibernate sessionFactory注册该类,如下所示: 第四步 。启动应用程序()后,“欢迎使用

  • 如何使用杰克逊序列化此类 Jackson没有拾取@XmlElementWrapper@XmlSee还有注释,Jackson也没有映射@XmlRootElement注释。我使用的是Jackson 1.9.0。Jackson正在将元素放入列表中,但没有映射POJO类的根元素。 这里是示例方法。 它生成的响应是'{“response”:{“status”:0,“PBBeans”:[{“user_name

  • 我有许多带有JAXB注释的实体,我希望使用消息转换器将它们转换为JSON。 我知道我的ObjectMapper可以读取JAXB注释: 但是当我调用rest服务时,默认的注册MappingJacksonHttpMessageConverter(它不是为读取JAXB而配置的)似乎会接管--当忽略@XMLTransient时,由于循环引用而导致stackoverflow... 如何配置Spring来使用

  • 我试图在类别和发布类(OneToMany)之间建立关系,我需要删除所有属于特定类别的发布。我不知道我是否遗漏了一些注释,但这是我在执行查询时的错误: org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常org.hibernate.exception.ConstraintViolationException:无法执行更新查询o

  • 我有两个不同的表,Person表和Employee表。我需要这两者之间的一一对应。Employee表的emp_id引用Person表的PERSON_ID。我需要一些帮助来编写使用注释的映射 persons.java