I would like to have a java class (MyClass) that holds an id, a name and a value, where the value can be a primitive type (string, int, long) or a list or a map. The class will be persisted by using JPA 2.0
My first thought were about a class with the following structure:
,-----------.
| MyClass |
|...........|
| id |
| name |
| index |
| key |
| value |
'------------
that resolve by getter and setter if a primitive or a list or a map ist stored – depending on the {index, key, value} combination.
An alternative would be an inheritance hierarchy:
.................
| AbstractClass |
,.- ................''--..__
_,-'' | `--.._
,-' | ................
...................... | |MapValueClass |
| PrimitiveValueClass| | ................
...................... |
..................
| ListValueClass |
..................
Keeping in mind, that I use JPA: is there a “standard way” or a “best practice” for this problem? Some way, that I can use myClass and access the value by a getter and decide with instanceof Map/List/Integer what sort of value is hold?
Well i found a solution that works fine for me: a base class and value holding derived classes:
Base class:
Scalar property class:
List property class:
… and don’t forget to add the classes to your persistence.xml