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

时间:2015-06-14 13:49:30

标签: hibernate grails grails-3.0

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

以下步骤对我不起作用。

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

第2步。正如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;
    }
}

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

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

  • grails run-app
  • 加载localhost:8080
  • 请注意“ARTEFACTS”
  • 部分下的“域名: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

1 个答案:

答案 0 :(得分:1)

正如Graeme建议的那样,解决方案是将hibernate.cfg.xml放入grails-app/conf而不是grails-app/conf/hibernate,否则配置将不会生效。我已在pull request提交了relevant documentation以反映这一点,我希望更新能够尽快生效,以防止其他用户遇到同样的问题。