Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [eclipselink-users] dynamic columns & maps?

Bryan,

Typically these types of dynamic domain models are handled with the database schema providing a secondary table for the entity where the Key/Value pairs from the attributes map is stored into this table in a separate row. The table can then be mapped using a basic-map mapping. This provides completely dynamic key/value pairs as well as querying support, although the querying may not be as optimal as a column on the source table.

In the case where you are willing to extend the table definition to contain additional columns for dynamic attributes I would assume the situation is much less dynamic as the schema needs to evolve. In these situations you could use a transformation mapping to define how the values from the row populate the object's attribute map and then extract these for writing directly into the row. This combined with direct query keys can address your requirements. The interesting part will be the maintenance of the meta-data involved for the dynamic columns. If this sounds interesting we can work through some sample code on the wiki.

Doug 

-----Original Message-----
From: Bryan Hunt [mailto:bhunt@xxxxxxx]
Sent: Wednesday, July 16, 2008 10:59 AM
To: EclipseLink User Discussions
Subject: [eclipselink-users] dynamic columns & maps?


I have an interesting use-case involving what I call dynamic columns  
and maps.  Consider the following class:

public void Foo
{
   private long id;
   private String name;
   private ...
   private HashMap<String, Object> attributes;
}

The objects stored in the map are basic types you can store in a  
database column such as Integer, String, Timestamp, etc.  I'd like to  
persist instances of Foo into a database table where each key of the  
attribute map corresponds to the name of a column in the table.  The  
set of keys, along with the type of data stored, is specified by a  
user-defined configuration and can change over the life of the  
project.  I also need to do queries against those key, value pairs and  
have instances of Foo returned from the database.  For example:

SELECT * FROM FOOS WHERE ERROR = 'Fatal Error'

for(Foo foo : foos)
   System.out.println(foo.getAttributes().get("ERROR")); // Will print  
"Fatal Error"

Can JPA / EclipseLink handle this use-case?

Bryan
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top