On 15/03/2007, at 12:15 AM, Andrus Adamchik wrote:

One last try at guessing it. On the server you have to do some magic to enable callbacks (hopefully eventually this will be more transparent). See "Enabling Callbacks" chapter at the bottom of this page:

http://cayenne.apache.org/doc/lifecycle-callbacks.html

if this doesn't help, please file a bug with code samples.

This is what we're doing... It seems to be in line with the above page. Any ideas?

public class DatabaseConnector {

        // Note: Maps is just a util class of mine that populates a HashMap
        private static final Map LIFECYCLE_CALLBACKS = Maps.asMap(
                new String[] {
"postLoad", "postPersist", "postRemove", "postUpdate", "prePersist", "preRemove", "preUpdate"
                },
                new Object[] {
                        new Integer( LifecycleListener.POST_LOAD ),
                        new Integer( LifecycleListener.POST_PERSIST ),
                        new Integer( LifecycleListener.POST_REMOVE ),
                        new Integer( LifecycleListener.POST_UPDATE ),
                        new Integer( LifecycleListener.PRE_PERSIST ),
                        new Integer( LifecycleListener.PRE_REMOVE ),
                        new Integer( LifecycleListener.PRE_UPDATE )
                }
        );

        private Number connectionStatus;
        private DataDomain dataDomain;
        private DataContext dataContext;

        public DatabaseConnector() {
                dataDomain = null;
                dataContext = null;
                setConnectionStatus( DB_CONNECTION_STATE_DISCONNECTED );
                try {
                        logger.info( "initializing configuration..." );
                        Configuration.initializeSharedConfiguration();
                        Configuration conf = 
Configuration.getSharedConfiguration();
                        dataDomain = conf.getDomain();
                        setupListeners();
                        // dataDomain.setTransactionDelegate(new
                        // DomainTransactionDelegate());
                        logger.info( "creating shared data context..." );
                        dataContext = dataDomain.createDataContext();
                        setConnectionStatus( DB_CONNECTION_STATE_ESTABLISHED );
                        logger.info( "Database connection established..." );
                }
                catch ( ConfigurationException e ) {
                        dataDomain = null;
                        dataContext = null;
                        setConnectionStatus( DB_CONNECTION_STATE_FAILED );
logger.error( "Configuration exception occured while connecting to database", e ); throw new RuntimeException( "Could not connect to database server. Please check the configuration." );
                }
                catch ( Exception e ) {
                        dataDomain = null;
                        dataContext = null;
                        setConnectionStatus( DB_CONNECTION_STATE_FAILED );
                        logger.error( "Exception occured while connecting to 
database", e );
throw new RuntimeException( "Could not connect to database server. Please check the logs." );
                }
        }

        protected void setupListeners()
        {
                LifecycleCallbackRegistry registry;
                Iterator callbacksIter;

                registry = dataDomain.getEntityResolver().getCallbackRegistry();
                callbacksIter = LIFECYCLE_CALLBACKS.keySet().iterator();
                while ( callbacksIter.hasNext() ) {
                        String aMethodName = ( String )callbacksIter.next();
                        Integer type = ( Integer )LIFECYCLE_CALLBACKS.get( 
aMethodName );
logger.debug( "add lifecycle callback:" + aMethodName + " type:" + type ); registry.addListener( type.intValue(), our.subclass.of.CayenneDataObject.class, aMethodName );
                }
                
        }
        <...>
}

with regards,
--

Lachlan Deck



Reply via email to