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;

@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 . 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
        "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 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(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


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.



+1


source







All Articles