I keep making postings about this... but that's because it keeps bugging me.
However, this time I'm going to suggest a solution.
In denormalised back end models, I want to be able to map the properties of
complex attributes (i.e. non-primitive) onto the same table as their parent.
In my previous example I mentioned the Quantity class we use that contains
an amount and a unit. So a cuboid object with a width, length and height
would have three attributes of type Quantity. Quantity in turn has two
primitive properties: amount (long) and unitid (int). I think I should be
able to map this with field descriptors that state the name of the property
in "dot notation" and the class they belong to, along the lines of:
<field-descriptor name="width.amount" class="Quantity"
column="width_amount"/>
<field-descriptor name="width.unitid" class="Quantity"
column="width_unitid"/>
This is easy from the proprerty-get point of view - and it avoids needing to
write FieldConversion objects to handle each type of nested attribute. It's
also easy from the reconstruction point of view - every time we hit a null
object going down (up?) the property tree we just instantiate based on the
classname in the field descriptor. This would remove my biggest bug-bear:
having to write a RowReader for every class that uses a nested attribute!
Below is the code I'm using to do just what I describe in JSP tags that read
and set nested attributes... what about implementing something like this in
the OJB PersistentFieldDefaultImpl and associated classes?
Cheers,
Gareth.
---code follows---
/**
* Returns a property value using dot notation, e.g.
object.property.subproperty
*/
public static Object getProperty( Object object, String propertyName )
throws Exception{
if( propertyName.indexOf('.') < 0 ){
Field field = object.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return field.get(object);
}
else{
String localName = propertyName.substring(0,
propertyName.indexOf("."));
Field field = object.getClass().getDeclaredField(localName);
field.setAccessible(true);
Object subObject = field.get(object);
String remainingPropertyName =
propertyName.substring(propertyName.indexOf('.') + 1);
return getProperty(subObject, remainingPropertyName);
}
}
/**
* Sets a property value using dot notation, instantiates new objects
along the
* way if necessary
*/
public static void setProperty( Object object, String propertyName, Object
value ) throws Exception{
if( propertyName.indexOf('.') < 0 ){
Field field = object.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
field.set(object, value);
}
else{
String localName = propertyName.substring(0,
propertyName.indexOf("."));
Field field = object.getClass().getDeclaredField(localName);
field.setAccessible(true);
Object subObject = field.get(object);
if( subObject == null ){
subObject = field.getType().newInstance();
field.set(object, subObject);
}
String remainingPropertyName =
propertyName.substring(propertyName.indexOf('.') + 1);
setProperty(subObject, remainingPropertyName, value);
}
}
---------------------------------------------
Gareth Cronin
Analyst/Programmer
Kiwiplan NZ Ltd: http://www.kiwiplan.com
Ph (64 9) 2727622 x854
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>