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 log4j.properties 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 log4j.properties 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(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2163)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2643)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.ontsys.learn.hibernate.HibernateLearner.doFirstUnitOfWork(HibernateLearner.java:22)
at com.ontsys.learn.hibernate.HibernateLearner.main(HibernateLearner.java:11)
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(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask.run(GooGooStatementCache.java:525)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

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="hbm2ddl.auto">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.  :)

Advertisements

  1. #1 by Jerry L on May 12, 2011 - 1:51 pm

    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.
    jl

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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