JPA and database Flex fields

1.6k Views Asked by At

'Flex fields' is a term for altering a table at the customer site to add extra columns to a table to hold custom pieces of information about that table's entity. I was wondering if anyone has dealt with supporting this mechanism with jpa, specifically eclipselink.

We wish to do this because we allow filtering of the base rows based on values in these customer specified fields, and having these fields in an association table causes multiple alias joins to this auxilliary table.

An obvious approach (at least it would seem to me) would be to define an aspect(s) that injects the new fields into the entity object, and then run dynamic weaving.

I was wondering if anyone has done this, and if there are any problems i'm not seeing, or suggestions about other approaches.

2

There are 2 best solutions below

3
On BEST ANSWER

So, if you inject new fields into your Entity how would you use these in your application? Would you also change your applications/UI's code?

The simplest solution is normally to have a Map of properties in your Entity, this allows new properties to be added at runtime, and allows for the application/UI to access and query these properties so that it can present them and allow editing and display. You can map the properties using an ElementCollection or OneToMany to a properties table.

If you wish the alter the existing table, that would be more complex, you would need to update the JPA mappings as well, either by editing the orm.xml and redeploying the application, or adding the mappings in a SessionCustomizer or DescriptorCustomizer. EclipseLink also supports a VIRTUAL AccessMode that allow mapping a column to a property instead of a field or get/set method.

A more brute force method is to update the object model code and the application code to make use of the new data.

EclipseLink also provides more dynamic solutions to map dynamic Entities to table without requiring classes.

See, http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic

2
On

I have started building an example that illustrates how EclipseLink can be used with extensible models (key-values table or flex-columns). It is a work in progress but I have started adding more diagrams and sample code to the example wiki page:

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Extensible

This also includes using the dynamic support James mentioned above.