2

'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.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66

2 Answers2

2

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

James
  • 17,965
  • 11
  • 91
  • 146
  • this entity's data is shown in a grid, and the columns displayed are user selectable, so the fetching of the data is thru reflection/or map based anyway. So the gui code really doesn't change, it just knows what fields are available and give the user the option as to what to show. – MeBigFatGuy Mar 02 '11 at 17:52
  • i do not want the properties table route, as we want to filter the base row based on values in these extra fields, and this requires multiple alias joins on the properties table. – MeBigFatGuy Mar 02 '11 at 17:58
  • Ah, i see the section Dynamic Configuration using API this looks promising, i'll accept this answer as a good one. Still scratching my head over it though, thanks. – MeBigFatGuy Mar 02 '11 at 18:03
2

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.

Doug Clarke
  • 550
  • 2
  • 6
  • it appears that your examples in the link show fields in a key/value association table. This is what we used to do, but we found the performance was terrible because we allow filtering of the base row based on the values of the key value assocation tables. that is why we are desiring to move these fields into the base row as first class columns. – MeBigFatGuy Mar 02 '11 at 17:56
  • Thanks Doug! This could be really useful to me! – Mark Robinson Mar 02 '11 at 18:02
  • The extensions approach includes 2 options for database storage. Values uses a separate table and Flex uses column in the primary table. Values does have performance and query challenges. – Doug Clarke Mar 03 '11 at 13:08