Search code examples
eclipselinkcustom-properties

How to add custom (user defined) properties to entities with EclipseLink?


I'd like to add user-defined custom fields to an existing entity in EclipseLink. For performance reasons, I want them to be stored directly in the entity's table, and I also want them to be "first class citizens", i.e. usable in queries.

From an implementation standpoint, the entity should have two methods to set and get custom fields:

public Object getCustomProperty(String key) { ... }
public void setCustomProperty(String key, Object value) { ... }

When setting a custom property foo, EclipseLink should store the value in the entity's table in a field named custom_foo.

From an end user standpoint, I would like to provide a GUI where the user can define and manage custom fields, which are then dynamically added to or removed from the database.

Is this possible in EclipseLink?

Regards, Jochen


Solution

  • Check out EclipseLink's Extensibility feature http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Extensible_Entities This with support for adding columns: http://wiki.eclipse.org/EclipseLink/DesignDocs/368365 seems somewhat like what you are asking for - except for the conflicting statement that it shouldn't store in the main table, then later state it should store in the entity's table "custom_foo" column.

    You will need to create the GUI that creates the mappings. Eclipselink ships with a metadata source implementation that reads from an orm.xml file, so you may have to write your own implementation for EclipseLink to use if your GUI cannot write to an orm file.