Mapping to Hibernate Annotations in Grails 3.0.1

How can I map a domain class to 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 to Hibernate Annotations I created a new class in src/main/com/books/Book.groovy

(also tried src/main/groovy/com/books/Book.groovy


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

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

    public Long getId() {
        return id;

    public void setId(Long 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 . We then registered the class with the Hibernate sessionFactory by adding the appropriate entries to the file grails-app/conf/hibernate/hibernate.cfg.xml

as follows:

<!DOCTYPE hibernate-configuration SYSTEM
        <mapping package="com.books" />
        <mapping class="com.books.Book" />


Step 4 . After running the application ( grails run-app

), the Welcome to Grails page ( grails-app/views/index.gsp

) reports null domain classes, which means the mapping did not take effect:

  • grails run-app
  • Loading localhost: 8080
  • Pay attention to "Domains: 0" under "ARTEFACTS"

Corresponding 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( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
       .hibernate.hql.internal.ast.tree.FromClause.addFromElement( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile( ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    ... 40 common frames omitted



source to share

1 answer

As Graham suggested, the solution is put hibernate.cfg.xml

in grails-app/conf

instead grails-app/conf/hibernate

, otherwise the configuration will not take effect. I submitted a request to reflect this in the related documentation and I hope that the update will take effect soon so that other users do not face the same issue.



All Articles