Hi Experts,
I need to extend entities at runtime by adding additional columns/properties to some of the instances. Take for example the Transaction entity given below:
Code:
Transaction:
- Type
- Date
- Amount
I might want to add a "Discount" column/property to all Transaction instances. This should be configured at runtime by the business client, and I've come up with different ways of possibly doing this.
The first method uses additional tables such as these:
Code:
Transaction extends ExtendableEntity
ExtensionDefinition:
- EntityName
- ExtensionName
- ExtensionType
ExtensionEntry:
- ExtensionDefinition (FK)
- ExtendableEntity (FK)
- Value
Because I want these Extensions to be available to quite a number of Entities I introduce the ExtendableEntity super class, which allows ExtensionEntry to contain a reference to any of the Entities that can be extended. With this method the column/property definition will be contained in ExtensionDefinition, and the actual value, one for each instance, in ExtensionEntry.
The alternative is to add a CLOB to ExtendableEntity which contains the additional columns/properties values in XML format, and to create a custom UserType for this CLOB:
Code:
Transaction extends ExtendableEntity
ExtendableEntity:
- CLOB
ExtensionDefinition:
- EntityName
- ExtensionName
- ExtensionType
The data contained in the CLOB will then look something like this:
Code:
<extensions>
<extension>
<id>12</id>
<value>Something</value>
</extension>
</extensions>
I still need the ExtensionDefinition Entity in order to define the additional columns/properties.
Between these two options, which would you recommend and why?
Also, I have one additional unresolved issue. I'd like to load the ExtensionDefinitions of an Entity whenever it is loaded, but there is no foreign key association between ExtensionDefinitions and ExtendableEntities, as the ExtensionDefinitions apply to the Entity itself rather than its instances. In other words, I want to perform a query such as this:
Code:
Select * from ExtensionDefinition where EntityName = '<insert Entity class name here>'
I was hoping that, although I can do that manually when loading Entities, Hibernate could help me with loading those values automatically so that fetching through association might work correctly (i.o.w something like EntityA.getEntityB().getExtensionDefinitions()).
Any and all advice will be appreciated.
Thank you,
Jaco