运行应用程序时出现Grails2.4和hibernate4错误

运行应用程序时出现Grails2.4和hibernate4错误,hibernate,grails,Hibernate,Grails,我已经将一个应用程序升级到Grails2.4.0,并且正在使用hibernate4插件。执行run app时,将使用内存中的数据库为每个域类生成以下错误示例。我在hibernate论坛上读过几篇文章,认为错误并不严重。它只是记录了一个错误,因为它试图删除的表还不存在 2014-Mai-24 13:25:26788错误[localhost-startStop-1]org.hibernate.tool.hbm2ddl.SchemaExport-SchemaExport.java 425-hh0003

我已经将一个应用程序升级到Grails2.4.0,并且正在使用hibernate4插件。执行run app时,将使用内存中的数据库为每个域类生成以下错误示例。我在hibernate论坛上读过几篇文章,认为错误并不严重。它只是记录了一个错误,因为它试图删除的表还不存在

2014-Mai-24 13:25:26788错误[localhost-startStop-1]org.hibernate.tool.hbm2ddl.SchemaExport-SchemaExport.java 425-hh000389:不成功:如果存在,则更改表用户角色删除约束FK_apcc8lxk2xnug8377fatvbn04

2014-Mai-24 13:25:26789错误[localhost-startStop-1]org.hibernate.tool.hbm2ddl.SchemaExport-SchemaExport.java 426-未找到表“用户角色”;SQL语句: alter table user_role drop constraint FK_apcc8lxk2xnug8377fatvbn04(如果存在)[42102-173]


有人知道如何停止日志噪音吗?

这是一个bug,看起来你可以这样做,不会造成任何问题,但是如果你不想看到这条消息,这里有一些解决方案:(编辑:选项2似乎工作得更好(见本文中的评论))

1.-DataSource.groovy中的单会话配置

2.-覆盖H2方言:

public class ImprovedH2Dialect extends H2Dialect {
    @Override
    public String getDropSequenceString(String sequenceName) {
        // Adding the "if exists" clause to avoid warnings
        return "drop sequence if exists " + sequenceName;
    }

    @Override
    public boolean dropConstraints() {
        // We don't need to drop constraints before dropping tables, that just
        // leads to error messages about missing tables when we don't have a
        // schema in the database
        return false;
    }
}

上面提供的@Luis解决方案也适用于MYSQL。只需扩展mysql5innodbdialent,如下所示:

import org.hibernate.dialect.MySQL5InnoDBDialect;

public class ImprovedMySQLDialect extends MySQL5InnoDBDialect {
    @Override
    public String getDropSequenceString(String sequenceName) {
        // Adding the "if exists" clause to avoid warnings
        return "drop sequence if exists " + sequenceName;
    }

    @Override
    public boolean dropConstraints() {
        // We don't need to drop constraints before dropping tables, that just leads to error
        // messages about missing tables when we don't have a schema in the database
        return false;
    }
}
然后在数据源文件中更改以下行:

dialect = org.hibernate.dialect.MySQL5InnoDBDialect

只要设置
dbCreate=“update”
,错误就会立即消失

问题是GORM(hibernate)试图删除H2 DB中从未创建过的表,因为每次运行应用程序时都会创建新的数据库。不幸的是,dbCreate默认设置为CREATEDROP,这对于在运行时动态创建的数据库来说确实没有意义

development {
    dataSource {
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }

你在使用spring安全插件吗?非常好!我用了#2。我从Hibernate3升级到Hibernate4(使用grails 2.3.11,但我认为这并不重要,并开始收到这些消息。非常烦人。因此,我很高兴发现这个Q&A.Option 2工作得很好。只需添加
dataSource.dialogue=com.mypackage.improvedh2dialogue
到您的数据源。Groovy只是为了澄清@10gitsandpaper的评论-我添加了
dialogue=“com.myPackage.improvedh2dial”
作为
dataSource.groovy
dataSource{}
的一个属性-谢谢!@andymccellough,我实际上想出了一个更简单的解决方案。为什么不把“create drop”改为“create drop”"@borjab我不使用
create
,因为
create
会破坏以前的数据。在开发过程中,我使用
BootStrap.groovy
加载测试数据。当我在服务器运行时更改域对象时,GORM会在重新编译和重新加载类后刷新模式。这会删除我加载的所有测试数据因此,我不得不重新启动应用程序,这会减慢我的速度并扼杀我的流量。
development {
    dataSource {
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }