Hey, while I'm pestering the mailing list, here's a quickie  for adding new
domain objects.  In appfuse, you add the object, you get tossed back to the
domain object list, and then if you want to do anything with your new
object, you re-select it from the list.

In some applications it would be nice to get back the new object identity
when it's inserted.  Maybe you want to keep working with that new object. 
Here's the usual blindingly straightforward way of doing this with Hibernate
%-|:-D

In your applicationContext-dao.xml, add a listener to hibernate that returns
back the ID to your object.  It's called (obviously)
IdTransferringMergeEventListener:

    <!-- Hibernate SessionFactory -->
    <bean id="idMergeListener"
class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation"
value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=${hibernate.dialect}
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.cache.use_second_level_cache=true
               
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
            </value>
            <!-- Turn batching off for better error messages under
PostgreSQL -->
            <!-- hibernate.jdbc.batch_size=0 -->
        </property>
        <property name="eventListeners">
            <map>
                <entry key="merge"><ref local="idMergeListener"/></entry>
            </map>
        </property>
    </bean>

What I've added here is a bean declaration of the listener (top line) and
then set the eventListeners property on the sessionFactory to that new bean. 
That should be it!  After a hibernate save, your domain object should have
its declared identity field automagically populated.

John Vance
-- 
View this message in context: 
http://www.nabble.com/How-to%3A-get-back-an-ID-on-inserting-with-Hibernate.-tp25618305s2369p25618305.html
Sent from the AppFuse - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@appfuse.dev.java.net
For additional commands, e-mail: users-h...@appfuse.dev.java.net

Reply via email to