Unsuccessful: alter table XXX drop constraint YYY in Hibernate/JPA/HSQLDB standalone

后端 未结 6 1542
忘掉有多难
忘掉有多难 2020-12-04 12:13

I am trying to run some Hibernate/JPA examples using an in-memory HSQL DB. The error message I get is the following:

13:54:21,427 ERROR SchemaExport:425 - HH         


        
6条回答
  •  旧巷少年郎
    2020-12-04 12:52

    The annoying error messages became more obnoxious stack traces at the start of every test using an in-memory database with HSQLDB and hbm2ddl.auto=create-drop.

    The response to an HSQLDB bug report suggested the use of DROP TABLE ... CASCADE rather than DROP CONSTRAINT IF EXISTS. Unfortunately the HSQLDB syntax for drop table is DROP TABLE

    [IF EXISTS] [RESTRICT | CASCADE]; Hibernate Dialect does not provide an easy mechanism for a Dialect to specify CASCADE following the final IF EXISTS clause. I wrote a bug for this limitation.

    However, I was able to overcome the issue by creating a custom Dialect as follows:

    public class HsqlDialectReplacement extends HSQLDialect {
    
      @Override
      public String getDropTableString( String tableName ) {
        // Append CASCADE to formatted DROP TABLE string
        final String superDrop = super.getDropTableString( tableName );
        return superDrop + " cascade";
      }
    
      @Override
      public boolean dropConstraints() {
          // Do not explicitly drop constraints, use DROP TABLE ... CASCADE
          return false;
      }
    
      @Override
      public Exporter
    getTableExporter() { // Must override TableExporter because it also formulates DROP TABLE strings synchronized( this ) { if( null == overrideExporter ) { overrideExporter = new HsqlExporter( super.getTableExporter() ); } } return overrideExporter; } private Exporter
    overrideExporter = null; private static class HsqlExporter implements Exporter
    { HsqlExporter( Exporter
    impl ) { this.impl = impl; } @Override public String[] getSqlCreateStrings( Table exportable, Metadata metadata ) { return impl.getSqlCreateStrings( exportable, metadata ); } @Override public String[] getSqlDropStrings( Table exportable, Metadata metadata ) { final String[] implDrop = impl.getSqlDropStrings( exportable, metadata ); final String[] dropStrings = new String[implDrop.length]; for( int i=0; i impl; }; }

    提交回复
    热议问题