Hbm2DLL.auto

The first thing that we should know about this Hibernate Configuration option is, never ever use that (Hbm2DLL.auto) option in production region. However, it can increase development speed in lower/dev regions, and that’s a good practice for large project that wanted to start from scratch – if they have a standard for database schema, and their methodology is Code-First development, but since there is no guarantee in data integrity for existed database schemas, it’s better and safer to avoid using this option in production region. So for legacy application, DON’T  use this option.

Validate: it won’t change the existed tables, but it validate and make sure the mapping file matches what we already have in database.

update: won’t drop, But it will add if the table is there/

create: create tables that are not in db, and drop the one which is already existed and create them again.

create-drop: like create option but it will drop all the tables it created when the SessionFactory closes.

This feature is useful only for development. Create your POJO classes, and then their mapping and that’s all. Then you can set this item in the hibernate.cfg.xml. Then go to database and export the DDL and put the DDL into your actual production environment.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">***********</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/MyDataBaseName</property>
  <property name="hibernate.connection.username">appuser</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.use_sql_comments">true</property>
  <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.format_sql">true</property>
  <mapping resource="com/navid/practice/hibernate/User.hbm.xml"/>
  <mapping resource="com/navid/practice/hibernate/Plan.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

Traditional Hibernate Issues (POJO): Part#4: ERROR: Unknown column

if you face “ERROR: Unknown column” exception in middle of Hibernate, check the XML mapping file and make sure the name of the pre-existed database tables are matched with what you have mentioned in the XML mapping file, for example:

Oct 29, 2016 2:09:22 PM org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: /* insert com.navid.practice.hibernate.User */ insert into USERS (NAME, TOTAL) values (?, ?)
Oct 29, 2016 2:09:22 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Oct 29, 2016 2:09:22 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'NAME' in 'field list'
org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2840)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3411)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:623)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:277)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:258)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:303)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:674)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:661)
at com.navid.practice.hibernate.Program.main(Program.java:23)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'NAME' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 22 more

Solution:

in the XML mapping file for the related table as mentioned in the exception , I had:

 <hibernate-mapping>
    <class name="com.navid.practice.hibernate.User" table="USERS">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="total" type="int">
            <column name="TOTAL" />
        </property>
    </class>
</hibernate-mapping>

AHA, now we found the culprit … so update the NAME to USERNAME , the field that we already defined in database side and re-run the application again.

found the issue in the highlighted part and updated it with what I have in the database side for that column. After fixing this issue and running the application, then I had the clean run:

INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Oct 29, 2016 2:13:01 PM org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: /* insert com.navid.practice.hibernate.User */ insert into USERS (USERNAME, TOTAL) values (?, ?)
Oct 29, 2016 2:13:02 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/protein_tracker]

Traditional Hibernate Issues (POJO): Part#3 : can’t find the database:

can’t find the database:

INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
 Oct 29, 2016 2:05:15 PM org.hibernate.dialect.Dialect 
 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
 Hibernate: /* insert com.navid.practice.hibernate.User */ insert into USERS (NAME, TOTAL) values (?, ?)
 Oct 29, 2016 2:05:15 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
 WARN: SQL Error: 1046, SQLState: 3D000
 Oct 29, 2016 2:05:15 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
 ERROR: No database selected
 org.hibernate.exception.GenericJDBCException: could not execute statement
 at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
 at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
 at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
 at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
 at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
 at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2840)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3411)
 at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
 at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:623)
 at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:277)
 at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:258)
 at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:303)
 at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)
 at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)
 at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
 at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
 at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
 at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
 at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
 at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
 at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
 at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:674)
 at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
 at org.hibernate.internal.SessionImpl.save(SessionImpl.java:661)
 at com.navid.practice.hibernate.Program.main(Program.java:23)
 Caused by: java.sql.SQLException: No database selected
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
 at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
 at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
 at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
 at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
 at org.hibernate.engine.jdb

Solution:

check the configuration xml file and you have to make sure, the database is mentioned in the url.

jdbc:mysql://localhost:3306/

Mine didn’t have that, so I added.

jdbc:mysql://localhost:3306/MyPersonalDatabaseName

Traditional Hibernate Issues (POJO): Part#1 org.hibernate.MappingException: Unknown entity

Unknown Entity exception might have a verity of reasons and causes, however, when you’re using POJO and XML files, since there is no Entity tags and mismatching between JPA nad Hibernate class tags, the reason of this exception might be a little bit challenging. when you’re using POJO with newer version of Hibernate, the main reason might be related to the way you are initiating SessionFactory.something like below…

 

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
 org.hibernate.MappingException: Unknown entity: com.navid.practice.hibernate.User
 at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
 at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1605)
 at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
 at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
 at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
 at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
 at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
 at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
 at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:674)
 at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
 at org.hibernate.internal.SessionImpl.save(SessionImpl.java:661)
 at com.navid.practice.hibernate.Program.main(Program.java:23)

the issue was related to the version of Hibernate that I had, 5.x and the code that I wrote for init the SessionFactory:

public class HibernateUtilities {

private static ServiceRegistry svcReg = null;
 private static SessionFactory sessionFactory = null;

static {

try {
 Configuration cfg = new Configuration().configure();

svcReg = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
 sessionFactory = cfg.buildSessionFactory(svcReg);

} catch (HibernateException e) {
 System.out.println("An Error in init the SessionFactory!");
 }

}

Solution:

the issue could be resolved just by changing the SessionFactory creation line …

sessionFactory = cfg.buildSessionFactory();

Traditional Hibernate Issues (POJO): Part#2 ERROR: No database selected

Having problems with the database configuration and XML mapping configurations.

 Oct 29, 2016 2:02:36 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections
 INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
 Oct 29, 2016 2:02:36 PM org.hibernate.dialect.Dialect
 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
 Hibernate: /* insert com.navid.practice.hibernate.User */ insert into USERS (NAME, TOTAL, ID) values (?, ?, ?)
 Oct 29, 2016 2:02:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
 WARN: SQL Error: 1046, SQLState: 3D000
 Oct 29, 2016 2:02:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
 ERROR: No database selected
 Oct 29, 2016 2:02:36 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
 INFO: HHH000010: On release of batch it still contained JDBC statements
 Oct 29, 2016 2:02:36 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
 ERROR: HHH000346: Error during managed flush [org.hibernate.exception.GenericJDBCException: could not execute statement]
 javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute statement
 at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
 at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
 at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
 at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1412)
 at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:476)
 at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3165)
 at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2379)
 at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
 at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
 at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
 at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
 at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
 at com.navid.practice.hibernate.Program.main(Program.java:26)
 Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement
 at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
 at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
 at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
 at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
 at org.hibernate.engine
 

the issue was resolved by changing the highelighed part to the proper part according to the database configuration.

<hibernate-mapping>
    <class name="com.navid.practice.hibernate.User" table="USERS">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="total" type="int">
            <column name="TOTAL" />
        </property>
    </class>
</hibernate-mapping>

 

Solution:

in database side, that table’s field = ID, was configuration as auto increment so I change that highlighted line to identity.

<generator class="identity" />