The tutorial was right after all

This week, I needed to quickly get a basic familiarity with Hibernate, so I did the tutorial in Section 2.1 of Java Persistence with Hibernate. The tutorial devoted several pages to setup and configuration. After creating:

  • a Hibernate XML mapping file
  • a main application class
  • a Hibernate XML configuration file
  • a helper class that would handle creating the SessionFactory
  • a configuration file

…I was getting antsy to try this out and see if it worked. I was using Maven instead of Ant, so I skipped the part about creating an Ant build file. I fired up HSQLDB, and after fixing up my file to be like the tutorial said, I got… a stack trace like the following:

09:37:40,501 WARN JDBCExceptionReporter:77 - SQL Error: -22, SQLState: S0002
09:37:40,501 ERROR JDBCExceptionReporter:78 - Table not found in statement [insert into MESSAGES (MESSAGE_ID, MESSAGE_TEXT, NEXT_MESSAGE_ID) values (null, ?, ?)]
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.ontsys.learn.hibernate.Message]
at org.hibernate.exception.SQLStateConverter.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.persister.entity.AbstractEntityPersister.insert(
at org.hibernate.persister.entity.AbstractEntityPersister.insert(
at org.hibernate.action.EntityIdentityInsertAction.execute(
at org.hibernate.engine.ActionQueue.execute(
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(
at org.hibernate.event.def.AbstractSaveEventListener.performSave(
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
at org.hibernate.impl.SessionImpl.fireSave(
at com.ontsys.learn.hibernate.HibernateLearner.doFirstUnitOfWork(
at com.ontsys.learn.hibernate.HibernateLearner.main(
Caused by: java.sql.SQLException: Table not found in statement [insert into MESSAGES (MESSAGE_ID, MESSAGE_TEXT, NEXT_MESSAGE_ID) values (null, ?, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.mchange.v2.c3p0.stmt.GooGooStatementCache$
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$

Help from the Online Tutorial

It looked like the schema was not being created. After messing around for a while, I was running out of ideas, so I went over to the online Hibernate tutorial to see if I could pick up a clue there. I found these lines in that tutorial’s example hibernate.cfg.xml configuration file:

<!-- Drop and re-create the database schema on startup -->
<property name="">create</property>

Aha! I added this to my project’s hibernate.cfg.xml file, and now the tutorial project worked as expected.

I had missed it…

Just as I was beginning to write up how the book’s tutorial had a horrible bug in it though, I noticed a section (p.63) titled Exporting the database schema. The text immediately preceding that section is

You now have an empty database that has no content, not even a schema. Let’s create the schema next.

The Exporting section then discusses different approaches to creating the schema, including the auto-create setting in the XML config file.  Oops!

The tutorial was right after all.  :)


One thought on “The tutorial was right after all

  1. Thanks,
    I had commented out that line, and forgotten about it.
    Things just didn’t go right afterward.
    Your post ended a debug session that had gone on for over an hour.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s