I am having troubles getting derby and hibernate to work together. I keep getting an exception saying that the Table 'HIBERNATE_UNIQUE_KEY' does not exist. Is this table supposed to created (ie do I need to manually create it or)? Any ideas on what I am doing wrong?
Hibernate version: hibernate-3.1
Mapping documents:
hibernate.cfg.xml
Code:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:/EVENT;create=true</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- JDBC connection pool -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2dll.auto">create</property>
<mapping resource="events/Event.hbm.xml" />
</session-factory>
</hibernate-configuration>
Event.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="events.Event"
table="EVENT"
>
<id
name="id"
column="EVENT_ID"
type="java.lang.Long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Event.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="date"
type="timestamp"
column="EVENT_DATE"
/>
<property
name="title"
type="java.lang.String"
column="title"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Event.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
Full stack trace of any exception that occurs:Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not get or update next value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:94)
at org.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
at org.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:61)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
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:544)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:533)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:529)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:292)
at $Proxy0.save(Unknown Source)
at events.EventManager.createAndStoreEvent(EventManager.java:40)
at events.EventManager.main(EventManager.java:17)
Caused by: SQL Exception: Table 'HIBERNATE_UNIQUE_KEY' does not exist.
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.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver30.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.id.TableGenerator.doWorkInCurrentTransaction(TableGenerator.java:131)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:64)
... 19 more
Name and version of the database you are using: Derby 10.1.2.1The generated SQL (show_sql=true):Here is the log info that I see before the exception is thrown:
11:36:20,460 INFO Environment:479 - Hibernate 3.1
11:36:20,460 INFO Environment:509 - hibernate.properties not found
11:36:20,476 INFO Environment:525 - using CGLIB reflection optimizer
11:36:20,476 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
11:36:20,554 INFO Configuration:1286 - configuring from resource: /hibernate.cfg.xml
11:36:20,554 INFO Configuration:1263 - Configuration resource: /hibernate.cfg.xml
11:36:20,695 INFO Configuration:468 - Reading mappings from resource: events/Event.hbm.xml
11:36:20,820 INFO HbmBinder:265 - Mapping class: events.Event -> EVENT
11:36:20,851 INFO Configuration:1397 - Configured SessionFactory: null
11:36:20,851 INFO Configuration:1022 - processing extends queue
11:36:20,851 INFO Configuration:1026 - processing collection mappings
11:36:20,851 INFO Configuration:1035 - processing association property references
11:36:20,851 INFO Configuration:1057 - processing foreign key constraints
11:36:20,929 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
11:36:20,929 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
11:36:20,929 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
11:36:21,210 INFO DriverManagerConnectionProvider:80 - using driver: org.apache.derby.jdbc.EmbeddedDriver at URL: jdbc:derby:/EVENT;create=true
11:36:21,210 INFO DriverManagerConnectionProvider:86 - connection properties: {user=, password=****}
11:36:21,742 INFO SettingsFactory:77 - RDBMS: Apache Derby, version: 10.1.2.1
11:36:21,742 INFO SettingsFactory:78 - JDBC driver: Apache Derby Embedded JDBC Driver, version: 10.1.2.1
11:36:21,773 INFO Dialect:103 - Using dialect: org.hibernate.dialect.DerbyDialect
11:36:21,788 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
11:36:21,788 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
11:36:21,788 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
11:36:21,788 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
11:36:21,788 INFO SettingsFactory:144 - Scrollable result sets: enabled
11:36:21,788 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled
11:36:21,788 INFO SettingsFactory:160 - Connection release mode: auto
11:36:21,788 INFO SettingsFactory:187 - Default batch fetch size: 1
11:36:21,788 INFO SettingsFactory:191 - Generate SQL with comments: disabled
11:36:21,788 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
11:36:21,788 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
11:36:21,804 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
11:36:21,804 INFO SettingsFactory:203 - Query language substitutions: {}
11:36:21,804 INFO SettingsFactory:209 - Second-level cache: enabled
11:36:21,804 INFO SettingsFactory:213 - Query cache: disabled
11:36:21,804 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.NoCacheProvider
11:36:21,804 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
11:36:21,804 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
11:36:21,804 INFO SettingsFactory:257 - Echoing all SQL to stdout
11:36:21,804 INFO SettingsFactory:264 - Statistics: disabled
11:36:21,804 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
11:36:21,804 INFO SettingsFactory:283 - Default entity-mode: pojo
11:36:21,851 INFO SessionFactoryImpl:153 - building session factory
11:36:22,210 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
11:36:22,226 INFO SessionFactoryImpl:353 - Checking 0 named HQL queries
11:36:22,226 INFO SessionFactoryImpl:373 - Checking 0 named SQL queries
11:36:22,679 WARN JDBCExceptionReporter:71 - SQL Error: 20000, SQLState: 42X05
11:36:22,679 ERROR JDBCExceptionReporter:72 - Table 'HIBERNATE_UNIQUE_KEY' does not exist.
11:36:22,679 WARN JDBCExceptionReporter:48 - SQL Warning: 10000, SQLState: 01J01
11:36:22,679 WARN JDBCExceptionReporter:49 - Database '/EVENT' not created, connection made to existing database instead.
EventManager Code:
Code:
package events;
import org.hibernate.Session;
import java.util.Date;
import java.util.List;
import util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
args = new String[] {"store"};
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
}
else if (args[0].equals("list")) {
List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() +
" Time: " + theEvent.getDate());
}
}
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
}
private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}
}
Event code:
Code:
package events;
import java.util.Date;
/**
*
* @hibernate.class table="EVENT"
*/
public class Event {
private Long id;
private String title;
private Date date;
public Event() {}
/**
*
* @hibernate.id column="EVENT_ID" generator-class="native"
*/
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
/**
*
* @hibernate.property column="EVENT_DATE" type="timestamp"
*/
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
/**
*
* @hibernate.property
*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}