22

I've upgraded an app to Grails 2.4.0, and I'm using the hibernate4 plugin. When executing run-app the error examples below are generated for each domain class using the in-memory database. I've read several posts on the hibernate forums that the errors aren't serious. It's simply logging an error because the table it's trying to drop doesn't yet exist.

2014-Mai-24 13:25:26,788 ERROR [localhost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport - SchemaExport.java 425 - HHH000389: Unsuccessful: 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 - Table "USER_ROLE" not found; SQL statement: alter table user_role drop constraint FK_apcc8lxk2xnug8377fatvbn04 if exists [42102-173]

Does anyone know how to stop the logging noise?

Community
  • 1
  • 1
Ken
  • 685
  • 2
  • 5
  • 11

3 Answers3

26

It's a bug, it seems that you can leave it that way and will cause no problem, but if you don't want to see the message here are some solutions: (Edit: Option 2 seems to work better (see comments in this post))

1.- singleSession configuration from DataSource.groovy

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

2.- overriding the H2 dialect:

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

Community
  • 1
  • 1
luisZavaleta
  • 1,160
  • 11
  • 21
  • 1
    Very nice! I used #2. I upgraded from Hibernate3 to Hibernate4 (using grails 2.3.11, but I don't think that matters and started getting these messages. Quite annoying. So, was glad to find this Q&A. – Nathan Ward Jul 20 '14 at 01:10
  • 5
    Option 2 worked great. Just add `dataSource.dialect = com.mypackage.ImprovedH2Dialect` to you DataSource.groovy – SGT Grumpy Pants Aug 11 '14 at 20:21
  • just to clarify to the @10GritSandpaper's comment - I added `dialect = "com.myPackage.ImprovedH2Dialect"` as a property of `dataSource{}` in `DataSource.groovy` - Thank you! – andy mccullough Jul 05 '15 at 20:41
  • @andymccullough , I actually figured out a much simpler solution posted below. – SGT Grumpy Pants Jul 06 '15 at 23:41
8

The Solution @Luis provided above works for MYSQL too. Just extend MySQL5InnoDBDialect instead like below:

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;
    }
}

Then in your datasource file change the following line:

dialect = org.hibernate.dialect.MySQL5InnoDBDialect

to

dialect = my.package.name.ImprovedMySQLDialect
Emmanuel John
  • 2,296
  • 1
  • 23
  • 30
8

Just set dbCreate="update", and the errors go away immediately.

The problem is that GORM (hibernate) is trying to drop tables in the H2 DB that have never been created because the DB is created new each time you run the app. Unfortunately, dbCreate is set to create-drop by default, which really doesn't make sense for a database that is created on the fly at runtime.

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"
    }
SGT Grumpy Pants
  • 4,118
  • 4
  • 42
  • 64
  • 1
    Why not just changing "create-drop" to "create" – borjab Mar 17 '16 at 10:12
  • @borjab I don't use `create` because `create` destroys the previous data. During development, I load test data with `BootStrap.groovy`. When I change a domain object while the server is running, GORM refreshes the schema after the class is recompiled and reloaded. That deletes all the test data that I load with bootstrap. So I end up having to restart the app, which slows me down and kills my flow. – SGT Grumpy Pants Mar 17 '16 at 22:17