title: Hibernate exception: can't prepare statement
body: <p>I am trying to insert a record into my database which is being handled by Hibernate (Apache Derby). I have a <em>User</em> object class which is was mapped using hbm.xml and the database is connecting fine, however returns the following error:</p>
  
  <pre><code>ERROR: Syntax error: Encountered "USER" at line 1, column 13. Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not prepare statement
  </code></pre>
  
  <p><strong>I am getting the following in my log:</strong></p>
  
  <pre><code>Dec 07, 2013 4:56:12 PM org.hibernate.annotations.common.Version &lt;clinit&gt;
  INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
  Dec 07, 2013 4:56:12 PM org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {4.2.8.Final}
  Dec 07, 2013 4:56:12 PM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: HHH000206: hibernate.properties not found
  Dec 07, 2013 4:56:12 PM org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: HHH000021: Bytecode provider name : javassist
  Dec 07, 2013 4:56:13 PM org.hibernate.cfg.Configuration configure
  INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
  Dec 07, 2013 4:56:13 PM org.hibernate.cfg.Configuration getConfigurationInputStream
  INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
  Dec 07, 2013 4:56:13 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
  WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
  Dec 07, 2013 4:56:13 PM org.hibernate.cfg.Configuration addResource
  INFO: HHH000221: Reading mappings from resource: titanmusicplayer/dal/mapping/User.hbm.xml
  Dec 07, 2013 4:56:13 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
  WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
  Dec 07, 2013 4:56:13 PM org.hibernate.cfg.Configuration addResource
  INFO: HHH000221: Reading mappings from resource: titanmusicplayer/dal/mapping/Song.hbm.xml
  Dec 07, 2013 4:56:13 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
  WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
  Dec 07, 2013 4:56:13 PM org.hibernate.cfg.Configuration addResource
  INFO: HHH000221: Reading mappings from resource: titanmusicplayer/dal/mapping/Playlist.hbm.xml
  Dec 07, 2013 4:56:13 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
  WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
  Dec 07, 2013 4:56:13 PM org.hibernate.cfg.Configuration doConfigure
  INFO: HHH000041: Configured SessionFactory: null
  Dec 07, 2013 4:56:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
  Dec 07, 2013 4:56:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000115: Hibernate connection pool size: 20
  Dec 07, 2013 4:56:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000006: Autocommit mode: false
  Dec 07, 2013 4:56:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000401: using driver [org.apache.derby.jdbc.EmbeddedDriver] at URL [jdbc:derby:/test;create=true]
  Dec 07, 2013 4:56:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000046: Connection properties: {user=root, password=****}
  Dec 07, 2013 4:56:16 PM org.hibernate.dialect.Dialect &lt;init&gt;
  INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyDialect
  Dec 07, 2013 4:56:16 PM org.hibernate.dialect.DerbyDialect &lt;init&gt;
  WARN: HHH000430: The DerbyDialect dialect has been deprecated; use one of the version-specific dialects instead
  Dec 07, 2013 4:56:16 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
  INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
  Dec 07, 2013 4:56:16 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory &lt;init&gt;
  INFO: HHH000397: Using ASTQueryTranslatorFactory
  Dec 07, 2013 4:56:17 PM org.hibernate.tuple.PojoInstantiator &lt;init&gt;
  INFO: HHH000182: No default (no-argument) constructor for class: titanmusicplayer.bll.Playlist (class must be instantiated by Interceptor)
  Dec 07, 2013 4:56:17 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 20000, SQLState: 42X01
  Dec 07, 2013 4:56:17 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: Syntax error: Encountered "USER" at line 1, column 13.
  Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not prepare statement
      at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:193)
      at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:89)
      at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.buildBatchStatement(AbstractBatchImpl.java:146)
      at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.getBatchStatement(AbstractBatchImpl.java:135)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3057)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3521)
      at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:393)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:385)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:301)
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:349)
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1159)
      at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
      at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
      at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
      at titanmusicplayer.dal.dao.UserModel.createUser(UserModel.java:49)
      at titanmusicplayer.TitanMusicPlayer.main(TitanMusicPlayer.java:73)
  Caused by: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "USER" at line 1, column 13.
      at org.apache.derby.impl.jdbc.SQLExceptionFactory40.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.EmbedPreparedStatement.&lt;init&gt;(Unknown Source)
      at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.&lt;init&gt;(Unknown Source)
      at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.&lt;init&gt;(Unknown Source)
      at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.&lt;init&gt;(Unknown Source)
      at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
      at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
      at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
      at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:96)
      at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:183)
      ... 17 more
  Caused by: java.sql.SQLException: Syntax error: Encountered "USER" at line 1, column 13.
      at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
      at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
      ... 32 more
  Caused by: ERROR 42X01: Syntax error: Encountered "USER" at line 1, column 13.
      at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
      at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source)
      at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
      at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
      at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
      ... 26 more
  </code></pre>
  
  <p><strong>My user object class:</strong></p>
  
  <pre><code>public class User {
  
      private long id;
      private String username;
      private String email;
      private String name;
      private long lastlogin;
  
      void user(long id, String username, String email, String name, long lastlogin) {
          this.id = id;
          this.username = username;
          this.email = email;
          this.name = name;
          this.lastlogin = lastlogin;
      }
  
      /* GETTERS */
  
      public long getId() {
          return this.id;
      }
  
      public String getUsername() {
          return this.username;
      }
  
      public String getEmail() {
          return this.email;
      }
  
      public String getName() {
          return this.name;
      }
  
      public long getLastlogin() {
          return this.lastlogin;
      }
  
      /* SETTERS */ 
  
      public void setId(long id) {
          this.id = id;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public void setLastlogin(long lastlogin) {
          this.lastlogin = lastlogin;
      }
  
  }
  </code></pre>
  
  <p><strong>My mapped model for Hibernate (xml):</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated Dec 7, 2013 4:55:39 PM by Hibernate Tools 3.4.0.CR1 --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="titanmusicplayer.bll.User" table="USER"&gt;
          &lt;id name="id" type="long"&gt;
              &lt;column name="ID" /&gt;
              &lt;generator class="assigned" /&gt;
          &lt;/id&gt;
          &lt;property name="username" type="java.lang.String"&gt;
              &lt;column name="USERNAME" /&gt;
          &lt;/property&gt;
          &lt;property name="email" type="java.lang.String"&gt;
              &lt;column name="EMAIL" /&gt;
          &lt;/property&gt;
          &lt;property name="name" type="java.lang.String"&gt;
              &lt;column name="NAME" /&gt;
          &lt;/property&gt;
          &lt;property name="lastlogin" type="long"&gt;
              &lt;column name="LASTLOGIN" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>My User model:</strong></p>
  
  <pre><code>public class UserModel  {
  
      public UserModel() {
      }
  
      public Session startSession() {
          Session s = SessionUtil.getSessionFactory().openSession();
          s.beginTransaction();
          return s;
      }
  
      private static void queryUser(Session session) {
          Query query = session.createQuery("from USER");                 
          List &lt;User&gt;list = query.list();
          java.util.Iterator&lt;User&gt; iter = list.iterator();
          while (iter.hasNext()) {
              User user = iter.next();
              System.out.println("Person: \"" + user.getName() +"\", " + user.getUsername() +"\", " + user.getEmail());
          }
  
          session.getTransaction().commit();
      }
  
      public static void createUser(Session session) {
          User user = new User();
          user.setName("Marcus");
          user.setLastlogin(0);
          user.setEmail("blah@gmail.com");
          user.setUsername(null);
          user.setId(0);
          session.save(user);
          session.getTransaction().commit();
      }
  
      public static List&lt;User&gt; checkLogin(Session s, String email, String password) {
          Query query = s.createQuery("from USER");
          List&lt;User&gt; list = query.list();
          return list;
      }
  
  }
  </code></pre>
  
  <p><strong>And I call it in Main like this:</strong></p>
  
  <pre><code>public static void main(String[] args) {
  
  
          UserModel um = new UserModel();
          Session s = um.startSession();
          UserModel.createUser(s);
  
      }
  </code></pre>
  
  <p>Any help on why I'm getting this exception would be appreciated. Been at this all morning and can't seem to resolve it.</p>
  
  <p>Thanks for suggestions / comments</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Now receiving this</p>
  
  <pre><code>Hibernate: insert into USERS (USERNAME, EMAIL, NAME, LASTLOGIN, ID) values (?, ?, ?, ?, ?)
  Dec 07, 2013 5:25:58 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 20000, SQLState: 42Y07
  Dec 07, 2013 5:25:58 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: Schema 'ROOT' does not exist
  </code></pre>