简体   繁体   English

在 Grails 3.0.1 中使用 Hibernate 注释进行映射

[英]Mapping with Hibernate Annotations in Grails 3.0.1

How can I map a domain class with annotations in Grails 3.0.1?如何在 Grails 3.0.1 中映射带有注释的域类?

The following steps didn't work for me.以下步骤对我不起作用。

Step 1 .第 1 步 I have created a new application with Grails 3.0.1 ( grails create-app books ).我用 Grails 3.0.1 ( grails create-app books ) 创建了一个新应用程序。

Step 2 .第 2 步 As described in Mapping with Hibernate Annotations I have created a new class in src/main/com/books/Book.groovy (tried src/main/groovy/com/books/Book.groovy as well)Mapping with Hibernate Annotations 中所述,我在src/main/com/books/Book.groovy创建了一个新类(也尝试过src/main/groovy/com/books/Book.groovy

package com.books;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Book {
    private Long id;
    private String title;
    private String description;
    private Date date;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Step 3 .第 3 步 Then registered the class with the Hibernate sessionFactory by adding relevant entries to the grails-app/conf/hibernate/hibernate.cfg.xml file as follows:然后通过将相关条目添加到grails-app/conf/hibernate/hibernate.cfg.xml文件来向 Hibernate sessionFactory 注册该类,如下所示:

<!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>

Step 4 .第 4 步 After starting the application ( grails run-app ), the 'Welcome to Grails' page ( grails-app/views/index.gsp ) reports zero domain classes, which means the mapping didn't take effect:启动应用程序( grails run-app )后,“欢迎使用 Grails”页面( grails-app/views/index.gsp )报告零域类,这意味着映射没有生效:

  • grails run-app grails 运行应用程序
  • Load localhost:8080加载本地主机:8080
  • Notice the 'Domains: 0' under the section 'ARTEFACTS'请注意“人工制品”部分下的“域:0”

Relevant exception in Grails 3.0.1 Grails 3.0.1 中的相关异常

If I query the above domain class, the following exception is thrown如果我查询上面的域类,则会抛出以下异常

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Book is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
       .hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:332) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    ... 40 common frames omitted

As Graeme suggested, the solution is putting hibernate.cfg.xml in grails-app/conf instead of grails-app/conf/hibernate , otherwise the configuration will not take effect.正如 Graeme 所建议的,解决方案是将hibernate.cfg.xml放在grails-app/conf而不是grails-app/conf/hibernate ,否则配置将不会生效。 I have submitted a pull request to reflect this in the relevant documentation and I hope the update to take effect soon so to prevent other users from facing the same issue.我已经提交了一个pull request来在相关文档中反映这一点,我希望更新尽快生效,以防止其他用户面临同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM