Hi Atipon,
atipon pongpat wrote:
> Hi, My company currently uses Toplink for o/r mapping, so I want to
> write an ojb mapping without any modification to existing domain
> object. The problem arises when I try to map nested object on the
> same table of parent object. My first solution is rowreader + field
> conversion.(Thanks to Thomas Mahler for his response to my friend,
> Sumeth Chittaprawat) It works fine but I just don't want to write a
> rowreader for every class that has a nested object. I decided to
> extend ojb provided PersistentField impl to supports using of nested
> attribute. My repository looks something like this.
Hey, this is a cool idea! I've never thought of this solution.
I think we should integrate it into the main distribution. As it will
solve a lot of problems.
> <field-descriptor
> id="2" name="name" column="SUPP_NAME" jdbc-type="CHAR" />
>
> <field-descriptor id="3" name="audit.createdBy" column="CREATED_BY"
> jdbc-type="CHAR" />
> Is there something I have to concern when I
> create my own PersistentField impl ?
No, no problems to be expected.
> I'm afraid that it cannot be
> used with odmg or jdo api.
I don't see any problems.
cheers,
Thomas
>
> Thanks in advance, Atipon Pongpat
>
> P.S. This my code. Any comments or suggestions are welcome. package
> testojb;
>
> import java.lang.reflect.Field;
>
> import org.apache.ojb.broker.metadata.MetadataException; import
> org.apache.ojb.broker.metadata.PersistentFieldMaxPerformanceImpl;
> import org.apache.ojb.broker.util.logging.Logger; import
> org.apache.ojb.broker.util.logging.LoggerFactory;
>
> /** * * This class supports using of nested attribute. * @author
> Atipon Pongpat */ public class
> PersistentNestedFieldMaxPerformanceImpl extends
> PersistentFieldMaxPerformanceImpl {
>
> private String fieldName;
>
> private Logger logger = LoggerFactory.getLogger(this.getClass());
>
> public PersistentNestedFieldMaxPerformanceImpl(Field f) { super(f); }
>
>
> public PersistentNestedFieldMaxPerformanceImpl(Class c, String
> fieldname) { super(c, fieldname); this.fieldName = fieldname; }
>
> /** * @see
> org.apache.ojb.broker.metadata.PersistentFieldMaxPerformanceImpl#getF-
> ieldRecursive(Class, String) */ protected Field
> getFieldRecursive(Class c, String fieldname) throws
> NoSuchFieldException { //thanks for the original implementation of
> PersistentFieldMaxPerformanceImpl try { Field f = null; int index =
> fieldname.indexOf("."); if (index >= 0) { String name =
> fieldname.substring(0, index); Field field =
> c.getDeclaredField(name); Class nestedClass = field.getType(); //
> String nestedName = fieldname.substring(index + 1); f =
> getFieldRecursive(nestedClass, nestedName); } else { f =
> c.getDeclaredField(fieldname); } return f; } catch
> (NoSuchFieldException e) { // if field could not be found in the
> inheritance hierarchy, signal error if (c == Object.class) { throw e;
> } // if field could not be found in class c try in superclass else {
> return getFieldRecursive(c.getSuperclass(), fieldname); } } }
>
> /** * @see org.apache.ojb.broker.metadata.PersistentField#get(Object)
> */ public Object get(Object anObject) throws MetadataException {
> return getNestedObject(anObject, fieldName); }
>
> /** * @see org.apache.ojb.broker.metadata.PersistentField#set(Object,
> Object) */ public void set(Object obj, Object value) throws
> MetadataException { setNestedObject(obj, fieldName, value); }
>
>
> /** * Set nested attribute with given value. * @param obj the object
> whose field should be modified * @param fieldName nested attribute
> name * @param value the new value for the field of obj being modified
> */ protected void setNestedObject(Object obj, String fieldName,
> Object value) {
>
> int index = fieldName.indexOf("."); if (index >= 0) { String name =
> fieldName.substring(0, index); PersistentFieldMaxPerformanceImpl
> pField = new PersistentFieldMaxPerformanceImpl(obj.getClass(), name);
> Object attrib = pField.get(obj);
>
> if (attrib == null) { try { attrib = pField.getType().newInstance();
> } catch (InstantiationException e) { throw new MetadataException(
> "Error instantiate field:" + name + " in object:" +
> obj.getClass().getName(), e); } catch (IllegalAccessException e) {
> throw new MetadataException( "Error getting field:" + name + " in
> object:" + obj.getClass().getName(), e); }
>
> pField.set(obj, attrib); } // String nestedName =
> fieldName.substring(index + 1);
>
> setNestedObject(attrib, nestedName, value); } else {
>
> PersistentFieldMaxPerformanceImpl pField = new
> PersistentFieldMaxPerformanceImpl( obj.getClass(), fieldName);
> pField.set(obj, value); }
>
> }
>
> /** * Get nested attribute with given field name. * @param obj object
> from which the represented field's value is to be extracted * @param
> fieldName nested attribute name * @return Object the value of the
> represented field in object obj */ protected Object
> getNestedObject(Object obj, String fieldName) {
>
> Object result = null; int index = fieldName.indexOf("."); if (index
> >= 0) { String name = fieldName.substring(0, index);
> PersistentFieldMaxPerformanceImpl pField = new
> PersistentFieldMaxPerformanceImpl(obj.getClass(), name); Object
> attrib = pField.get(obj);
>
> if (attrib != null) { // String nestedName =
> fieldName.substring(index + 1); result = getNestedObject(attrib,
> nestedName); } } else {
>
> PersistentFieldMaxPerformanceImpl pField = new
> PersistentFieldMaxPerformanceImpl( obj.getClass(), fieldName); result
> = pField.get(obj); }
>
> return result; }
>
> }
>
>
>
>
> --------------------------------- Do you Yahoo!? Y! Web Hosting - Let
> the expert host your web site
--
To unsubscribe, e-mail: <mailto:ojb-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:ojb-user-help@;jakarta.apache.org>
- Need PersistentField impl suggestion atipon pongpat
- Re: Need PersistentField impl suggestion Jakob Braeuchi
- Re: Need PersistentField impl suggestion Thomas Mahler
- Re: Need PersistentField impl suggestion Jakob Braeuchi
- Re: Need PersistentField impl suggestion Thomas Mahler
- Need PersistentField impl suggestion atipon pongpat
- Re: Need PersistentField impl sugge... Jakob Braeuchi
- Designing application Roland Carlsson
- RE: Designing application Ong Lee Haw (FS)
- Re: Designing application David Leal
- Re: Designing application Affan Qureshi
- Re: Designing application Kevin Viet
- Re: Designing application Eliot Sykes
