hbm2ddl.auto is set to “UPDATE”, but its trying to create table before every insertion

Correct me if i am wrong!.
Generally, if hbm2ddl.auto is set to “UPDATE”, hibernate checks if the table exists in db or not, if not it creates that table and pushes the data, if table exists it just adds data to that table.
for hbm2ddl.auto = update, What i observed is, before every insertion, hibernate is trying to create the table. though the table exists, it throws exception in log and then hibernate come to know that about the existence of table.
is this a normal behavior ? or i am missing something ?

please find below log generated with the help of log4j in console..

Hibernate: create table UsersCC (userID int4 not null, userDOB date, userEmail varchar(255), userPswd varchar(255), primary key (userID))
23:02:27,799  WARN ExceptionHandlerLoggedImpl:27 - GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL 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:524)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:470)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:273)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:203)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:110)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at com.hibernate.Setup.SetupTest.main(SetupTest.java:19)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "userscc" already exists
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2310)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2023)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:217)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:421)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:318)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:310)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 13 more
Hibernate: insert into UsersCC (userDOB, userEmail, userPswd, userID) values (?, ?, ?, ?)
23:02:27,998 TRACE BasicBinder:65 - binding parameter [1] as [DATE] - [Sun Jan 01 23:02:25 IST 2017]
23:02:27,999 TRACE BasicBinder:65 - binding parameter [2] as [VARCHAR] - [[email protected]]
23:02:28,000 TRACE BasicBinder:65 - binding parameter [3] as [VARCHAR] - [lenovo18]
23:02:28,004 TRACE BasicBinder:65 - binding parameter [4] as [INTEGER] - [101]
23:02:28,013  INFO pooling:230 - HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/campusCafeDb]

if its normal, then isn’t it a performance issue ?

Answer

hbm2ddl.auto Validate : If the value is validate then hibernate only validates whether the table and columns are exist or not. If the table doesn’t exist then hibernate throws an exception. Validate is the default value for hbm2ddl.auto.

hbm2ddl.auto update : If the value is update then hibernate checks for the table and columns. If table doesn’t exist then it creates a new table and if a column doesn’t exist it creates new column for it.

This link has beautiful explanation on all of the values.

http://www.onlinetutorialspoint.com/hibernate/hbm2ddl-auto-example-hibernate-xml-config.html

none is unofficial value i guess. I found it somewhere in stackoverflow but I forgot where.

Leave a Reply

Your email address will not be published. Required fields are marked *