Mapping with Hibernate Annotations in Grails 3.0.1

夙愿已清 提交于 2019-12-04 02:16:11

问题


How can I map a domain class with annotations in Grails 3.0.1?

The following steps didn't work for me.

Step 1. I have created a new application with Grails 3.0.1 (grails create-app books).

Step 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)

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. Then registered the class with the Hibernate sessionFactory by adding relevant entries to the grails-app/conf/hibernate/hibernate.cfg.xml file as follows:

<!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. 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
  • Load localhost:8080
  • Notice the 'Domains: 0' under the section 'ARTEFACTS'

Relevant exception in 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

回答1:


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



来源:https://stackoverflow.com/questions/30830155/mapping-with-hibernate-annotations-in-grails-3-0-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!