At 10:36 2002-12-04 +0100, you wrote:
Hi all
i've been looking through the tutorials to do inheritance mapping of a whole inheritance tree in a single table
and i've seen that it is _required_ to have a field in the objects that contains the class name ...

why is it so ?
shouldn't it be possible to tell OJB to call getClass().getName() on its own for the FieldDescriptor storing the instance type in db ?
has somebody thought a solution for this (e.g. write a PersistentField implemenation that can do it) ?

Thanks for your time

David
hi again,
actually i've been thinking of a possible PersistentField implementation (see attached file)
it seems very simple, (so simple it feels weird ;) )

what do you think ?

thanks
David
package com.sysdeo.framework.persistencelayer.objectbridge;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache",
 *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.apache.ojb.broker.metadata.ClassDescriptor;
import org.apache.ojb.broker.metadata.MetadataException;
import org.apache.ojb.broker.metadata.PersistentField;
import org.apache.ojb.broker.metadata.PersistentFieldDefaultImpl;
import org.apache.ojb.broker.util.logging.Logger;
import org.apache.ojb.broker.util.logging.LoggerFactory;

/**
 * This class is a wrapper around java.lang.reflect.Field objects.
 * @author <a href="mailto:[EMAIL PROTECTED]";>Thomas Mahler<a>
 * @version $Id: PersistentFieldDynamicClassNameImpl.java,v 1.8 2002/09/21 15:49:37 
brj Exp $
 */
public class PersistentFieldDynamicClassNameImpl extends PersistentFieldDefaultImpl
{
        public PersistentFieldDynamicClassNameImpl(Field f)
        {
                super(f);
        }

        public PersistentFieldDynamicClassNameImpl(Class c, String fieldname)
        {
                super(c, fieldname);
        }

        /** 
         * Sets the field represented by this PersistentField object on the specified 
object argument to the specified new value.
         * The new value is automatically unwrapped if the underlying field has a 
primitive type.
         * This implementation invokes set() on its underlying Field object if the 
argument <b>is not null</b>.
         * OBS IllegalArgumentExceptions are wrapped as PersistenceBrokerExceptions.
         * 
         * @throws MetadataException if there is an error setting this field value on 
obj
         * @see java.lang.reflect.Field
         */
        public void set(Object obj, Object value)
        {
                if (!isConcreteClassField()) {
                        super.set(obj, value);
                }
        }

        /**
         * Returns the value of the field represented by this PersistentField, on the 
specified object.
         * This implementation invokes get() on its underlying Field object.
         *
         * @param obj - the object instance which we are trying to get the field value 
from
         * @throws MetadataException if there is an error getting this field value 
from obj
         * @see java.lang.reflect.Field
         */
        public Object get(Object obj)
        {
                if (isConcreteClassField()) {
                        return obj.getClass().getName();
                } else {
                        return super.get(obj);
                }
        }
        
        /**
         * Determines whether this field represent the OJB special field that contains 
         * the concrete class name if persistent storage
         * This implementation checks the field name with the {@link 
ClassDescriptor#OJB_CONCRETE_CLASS} value.
         *
         * @param obj - the object instance which we are trying to get the field value 
from
         */
        private boolean isConcreteClassField() {
                return ClassDescriptor.OJB_CONCRETE_CLASS.equals(getField().getName());
        }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to