小编典典

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

hibernate

如何在Grails 3.0.1中使用注释映射域类?

以下步骤对我不起作用。

步骤1 。我使用Grails 3.0.1(grails create-app books)创建了一个新应用程序。

第二步 。如“ 使用hibernate注释映射”中所述,我在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;
    }
}

第三步 。然后通过向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>

第四步 。启动应用程序(grails run-app)后,“欢迎使用Grails”页面(grails- app/views/index.gsp)报告了零域类,这意味着映射未生效:

  • Grails运行应用
  • 加载本地主机:8080
  • 请注意“ ARTEFACTS”部分下的“ Domains:0”

Grails 3.0.1中的相关异常

如果我查询上述域类,则抛出以下异常

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

阅读 239

收藏
2020-06-20

共1个答案

小编典典

作为格雷姆认为,该解决方案将hibernate.cfg.xmlgrails-app/conf代替grails- app/conf/hibernate,否则配置将不会生效。我已经提交了一个拉取请求,以在相关文档中反映出来,希望此更新尽快生效,以防止其他用户遇到相同的问题。

2020-06-20