Search code examples
databasehibernatejava-8derby

Hibernate: Caused by: java.sql.SQLException: Table/View 'USERS' already exists in Schema


I am on Java SE 8, and I use the Hibernate ORM for persistence.

  • Hibernate 5.3.6.Final
  • Derby DB 10.14.2.0

The Hibernate Config:

<property name="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property><!-- update vs validate-->

The persistence entity, Users DB table, has the following key:

    @Id
    @Column(name = "email", nullable = false, unique = true, updatable = false)
    private String email = "";

When I start the app I get the following information:

 May 23, 2019 10:53:16 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.2.Final}
May 23, 2019 10:53:17 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
May 23, 2019 10:53:17 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
May 23, 2019 10:53:19 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: org.apache.derby.jdbc.EmbeddedDriver at URL: jdbc:derby:appName-v1.db;create=false;useSSL=false
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001001: Connection properties: {user=user, password=****}
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001003: Autocommit mode: false
May 23, 2019 10:53:19 PM com.mchange.v2.log.MLog 
INFO: MLog clients using java 1.4+ standard logging.
May 23, 2019 10:53:20 PM com.mchange.v2.c3p0.C3P0Registry 
INFO: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
May 23, 2019 10:53:20 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001007: JDBC isolation level: <unknown>
May 23, 2019 10:53:20 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@c07c305b [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@36772381 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 250, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, identityToken -> 1hge17oa21s08ng61w9xxa8|40b5cb4d, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 60, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@59324586 [ description -> null, driverClass -> null, factoryClassLocation -> null, forceUseNamedDriverClass -> false, identityToken -> 1hge17oa21s08ng61w9xxa8|73f7aa9e, jdbcUrl -> jdbc:derby:appName-v1.db;create=false;useSSL=false, properties -> {user=******, password=******} ], preferredTestQuery -> null, privilegeSpawnedThreads -> false, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, extensions -> {}, factoryClassLocation -> null, identityToken -> 1hge17oa21s08ng61w9xxa8|71ff733a, numHelperThreads -> 3 ]
May 23, 2019 10:53:22 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@72bcd19] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))

Then I get Exceptions:

WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at mr.m.v.dataBase.HibernateBOProcessor.<init>(HibernateBOProcessor.java:49)
    at mr.m.v.dataBase.HibernateBOProcessor.<clinit>(HibernateBOProcessor.java:28)
    at mr.m.v.security.authJaas.steady.DBLoginModule.login(DBLoginModule.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.security.auth.login.LoginContext.invoke(Unknown Source)
    at javax.security.auth.login.LoginContext.access$000(Unknown Source)
    at javax.security.auth.login.LoginContext$4.run(Unknown Source)
    at javax.security.auth.login.LoginContext$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.login.LoginContext.invokePriv(Unknown Source)
    at javax.security.auth.login.LoginContext.login(Unknown Source)
    at mr.m.v.security.authJaas.JaasLoginContext.login(JaasLoginContext.java:103)
    at mr.m.v.gui.MainUI$1.lambda$0(MainUI.java:227)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Table/View 'USERS' already exists in Schema 'app'.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at com.mchange.v2.c3p0.impl.NewProxyStatement.execute(NewProxyStatement.java:75)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 29 more
Caused by: ERROR X0Y32: Table/View 'USERS' already exists in Schema 'app'.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.duplicateDescriptorException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.addDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.execute.CreateTableConstantAction.executeConstantAction(Unknown Source)
    at org.apache.derby.impl.sql.execute.MiscResultSet.open(Unknown Source)
    at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
    at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
    ... 34 more

Hibernate: alter table app.Users drop constraint UK_bnk3qe2j3b69p30frfqk2i76d
Hibernate: alter table app.Users add constraint UK_bnk3qe2j3b69p30frfqk2i76d unique (uuid)

What I do not like about it, is that it tries to re-create the DB table each time I start the app up. It does so when the hibernate.hbm2ddl.auto is set to update. If I set it to the validate, the Hibernate complains that the DB table does not exist. The create option is not viable either, it drops the table and re-creates it. So, I want to keep the DB tables that I have created already. So, I need either update, or validate as for the value.

On the older version of the Hibernate the update option met my requirements.

I wonder whether or not the exceptions might be an acceptable outcome. As, for example, on an application server, if the user authentication fails, you get an exception (on the console on an IDE) as a valid indication of authentication failure. To name only few...


Solution

  • I've figured out the work around. It appears that the solution is to set the hibernate.hbm2ddl.auto to none. Then the app runs as expected, and does the DB communication. That is, does not try to re-create the table.

    Yet, if the DB has no tables, and you want to create them at run-time (with Hibernate), the key can be set, for example, to update. But next time you start the app up, set the key to none, as long all the tables that you want the system has created already.