运行应用程序的Grails 2.4和hibernate4错误

Ken*_*Ken 20 grails hibernate

我已经将应用程序升级到Grails 2.4.0,我正在使用hibernate4插件.执行run-app时,使用内存数据库为每个域类生成以下错误示例.我在hibernate论坛上看了几篇关于错误并不严重的帖子.它只是记录错误,因为它试图丢弃的表尚不存在.

2014-Mai-24 13:25:26,788 ERROR [localhost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport - SchemaExport.java 425 - HHH000389:不成功:alter table user_role drop constraint FK_apcc8lxk2xnug8377fatvbn04 if exists

2014-Mai-24 13:25:26,789 ERROR [localhost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport - SchemaExport.java 426 - 表"USER_ROLE"未找到; SQL语句:alter table user_role drop constraint FK_apcc8lxk2xnug8377fatvbn04 if exists [42102-173]

有谁知道如何阻止伐木噪音?

lui*_*eta 24

这是一个错误,似乎你可以这样离开并且不会造成问题,但是如果你不想看到这里的消息是一些解决方案:(编辑:选项2似乎更好用(见本文中的评论) ))

1.-来自DataSource.groovy的singleSession配置

https://jira.grails.org/browse/GRAILS-11198

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

不成功:alter table XXX在Hibernate/JPA/HSQLDB独立版中删除约束YYY

  • 方案2运作良好.只需向您添加`dataSource.dialect = com.mypackage.ImprovedH2Dialect` DataSource.groovy (5认同)

10G*_*per 8

只需设置dbCreate="update",错误就会立即消失.

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

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"
    }
Run Code Online (Sandbox Code Playgroud)


Emm*_*ohn 7

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的数据源文件中更改以下行:

dialect = org.hibernate.dialect.MySQL5InnoDBDialect
Run Code Online (Sandbox Code Playgroud)

dialect = my.package.name.ImprovedMySQLDialect
Run Code Online (Sandbox Code Playgroud)