在Grails 3.0.1中映射Hibernate注释 [英] Mapping with Hibernate Annotations in Grails 3.0.1

查看:72
本文介绍了在Grails 3.0.1中映射Hibernate注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下步骤对我来说不起作用。

我如何在Grails 3.0.1中映射带注解的域类?



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



第2步。如映射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;

@实体
公开课预订{
私人长ID;
私有字符串标题;
私有字符串描述;
私人日期日期;

@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 )后,'Welcome to Grails'页面( grails-app / views / index.gsp $ b


  • grails run-app

  • 载入本地主机:8080

  • 注意'ARTEFACTS'部分下的'Domains:0'



Grails 3.0.1中的相关异常



如果我查询上面的域类,会抛出以下异常:

 原因:org.hibernate.hql.internal.ast.QuerySyntaxException:书未映射到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]
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)〜[hibernate-core-4.3.8.Final.jar:4.3 .8.Final]
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
在org.hibernate.h ql.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个常见框架省略
hibernate.cfg.xml 放入 c>在 grails-app / conf 而不是 grails-app / conf / hibernate 中,否则配置不会影响。我已提交拉取请求,以反映相关文档,我希望更新即将生效,以防止其他用户面临同样的问题。


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

解决方案

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.

这篇关于在Grails 3.0.1中映射Hibernate注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆