Generic Attributes
The features described in this chapter have not evolved for a while now
and therefore tend to be a little out of date.
Beside the standard mapping explained in the Introduction, there can also be generic
extension attributes defined in entity classes which are stored in the database
in a separate table. This table can be accessed either the ordinary way using
the entity class de.mathema.pride.Attribute or by extended descriptors
to embed the generic attributes in specific entity types. The following
example shows an extension of class Customer from the introduction
by a generic attribute solvency:
class Customer extends MappedObject {
// attributes, descriptor and access methods
like above
// Additional datamember and its access methods
private String solvency;
public String getSolvency() { return solvency;
}
public void setSolvency(String val) { solvency
= val; }
// Extension descriptor and its access method
protected static ExtensionDescriptor xd
= new ExtensionDescriptor
(red, new String[][]
{
{ "solvency", "getSolvency", "setSolvency" }
});
protected ExtensionDescriptor getXDescriptor()
{ return xd; }
}
|
Reading and writing of generic attributes is not performed automatically
during reading and writing of other attributes but must explicitely be called
by the functions fetchx(), createx() and updatex().
The function updatex(String) allows to update a single attribute
to minimize database interactions. The class ExtensionDescriptor
allows to map generic attributes to data members of an entity class. Its
structure is pretty similar to the one for standard mappings. Its constructor
requires
- The RecordDescriptor if the entity type
- A 2-dimensional string array in the same style as for standard
mapping
Although seldomly used, generic attributes are much more flexible and
have some advantages:
- Derivation in the database
Although the standard mapping approach allows entity derivation, the records
of derived types must be stored in different databases tables. Generic attributes
provide full, transparent polymorphism.
- Extension of entities without database schema modification
Generic attributes are stored type-neutral as separate records in table
attribute. An extension of an entity class by additional attributes
does not require any changes of the database schema but just causes appropriate
additional records to appear in the attribute table.
- Variable number of attributes
Generic attributes are also of interest if the number and structure of
attributes is not known at compile time. However, in this case you can't
use a static initialization as this relies on a fix structure.
As a drawback, generic attributes potentially take a lot more space in
the database and a higher number of database interactions. They should therefore
only be used if the number of records and attributes is small and interactions
are not time-critical.