I'm building a Hibernate (3.4.0.GA) web-app using Mystic Paste as a template (the source is available at
http://kenai.com/projects/mystic-apps ). The Mystic Paste objects save fine, but my objects do not. I've discovered that the problem occurs in org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(). The problem is at lines 129-131, which say
Code:
129. else if ( generatedId == IdentifierGeneratorFactory.POST_INSERT_INDICATOR ) {
130. return performSave( entity, null, persister, true, anything, source, requiresImmediateIdAccess );
131. }
The Mystic Paste objects have generatedId == IdentifierGeneratorFactory.POST_INSERT_INDICATOR, but my objects just have a generatedId of some Long sequence value. The generatedId variable comes from lines 121-122, which read
Code:
121. EntityPersister persister = source.getEntityPersister( entityName, entity );
122. Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );
The "source" variable here refers to the session and the "entity" variable is the object to be saved. When I step through the code, both my object and the MysticPaste object return a SingleTableEntityPersister at line 121. However, at line 122 the Mystic Paste object returns an IdentityGenerator while my object returns a SequenceGenerator. This seems to be the problem, since the IdentityGenerator's generate() function returns POST_INSERT_INDICATOR, while the SequenceGenerator's generate() just returns a Long.
So my question is, where are these Generators set? And how can I get the session for my object to have an IdentityGenerator here? The session factory definitions for the Mystic Paste object and for my object are below. As far as I can tell, the only difference between the two is that the Mystic Paste object is using an HSQL database while mine is using an Oracle database.
Code:
<!-- Mystic Paste Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.jdbc.batch_size">1000</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
</props>
</property>
</bean>
<!-- My Hibernate session factory -->
<bean id="sessionFactoryOracle"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSourceOracle"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.jdbc.batch_size">1000</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
</props>
</property>
</bean>