我已将应用升级到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
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如果存在[42102-173]
有谁知道如何阻止伐木噪音?
答案 0 :(得分: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;
}
}
Unsuccessful: alter table XXX drop constraint YYY in Hibernate/JPA/HSQLDB standalone
答案 1 :(得分: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;
}
}
然后在您的数据源文件中更改以下行:
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
到
dialect = my.package.name.ImprovedMySQLDialect
答案 2 :(得分:7)
只需设置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"
}