Reputation: 462
I have a data structure that boils down to:
class StylesheetUserPreferencesImpl {
long id;
String desc;
Map<String, Map<String, String>> nodeAttributes;
}
To try and make the JPA2 annotations a little more sane I created small class to represent the inner class:
class LayoutAttributeImpl {
String nodeId;
private Map<String, String> attributes;
}
Which gives me:
class StylesheetUserPreferencesImpl {
long id;
String desc;
Map<String, LayoutAttributeImpl> nodeAttributes;
}
What I would like in the end is a table structure that looks like:
SS_USER_PREFS
PREFS_ID
DESC
SS_LAYOUT_ATTRS
PREFS_ID
NODE_ID
NAME
VALUE
I'm not really sure how to go about mapping this in JPA though. It seems like I want LayoutAttributeImpl to be Embeddable but as far as I understand Embeddable objects can't contain collections. I have it working right now with LayoutAttributeImpl acting as a full-fledged @Entity but that gives me an extra table that I really don't need.
Upvotes: 3
Views: 1006
Reputation: 47183
How about something like:
class StylesheetUserPreferencesImpl {
long id;
String desc;
Map<AttributeCoordinate, String> attributes;
}
class AttributeCoordinate {
String nodeID;
String prefID;
}
That maps very straightforwardly onto the tables you want - Embeddables can be keys in maps, right? If you're always looking up attributes with both a node and a preference ID at the same time, then you can hide this slightly weird object structure inside the getter.
If you want to be able to manipulate whole maps of an individual node's attributes, you have a problem. You could write a Map implementation that fakes it: it contains a node ID, and handles lookups by treating the key as a preference ID, then doing a lookup with the pair of them.
Does that make any sense? I have just eaten a lot of pancakes, and it's possible i'm not thinking straight.
Upvotes: 1