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.
<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 ? I'm
afraid that it cannot be used with odmg or jdo api.
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#getFieldRecursive(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