I've been trying to determine if there is a standard way to map a "collection of collections". Not really sure if this is a traditional ternary relationship, or something else. Here's the idea...
Table definitions:
Code:
CREATE TABLE entity_a (
a_id NUMBER NOT NULL,
constraint "entity_a_pk" primary key ("a_id")
);
CREATE TABLE a_b (
a_id NUMBER NOT NULL,
b_id NUMBER NOT NULL,
list_index NUMBER NOT NULL,
map_key VARCHAR2(255) NOT NULL,
constraint "a_b_pk" primary key ("a_id","b_id","list_index")
);
CREATE TABLE entity_b (
b_id NUMBER NOT NULL,
constraint "entity_b_pk" primary key ("b_id")
);
Sample Data:Code:
|----------|
| entity_a |
|----------|
| a_id |
|------|
| 1 |
|-----|
| a_b |
|-----|
| a_id || b_id || list_index || map_key |
|------||------||------------||---------|
| 1 || 2 || 0 || foo |
| 1 || 3 || 1 || foo |
|----------|
| entity_b |
|----------|
| b_id |
|------|
| 2 |
| 3 |
Java classes (using annotations):Code:
@Entity @Table(name = "entity_a")
public class EntityA {
@Id @Column(name = "a_id")
private Integer id;
@OneToMany
@JoinTable(name = "a_b", joinColumns = {@JoinColumn(name = "a_id")}, inverseJoinColumns = {@JoinColumn(name = "b_id")})
@MapKey(columns = @Column(name = "map_key"))
//what else goes here?
private Map<String, List<EntityB>> mapList;
}
@Entity @Table(name = "entity_b")
public class EntityB {
@Id @Column(name = "b_id")
private Integer id;
}
If this mapping is possible, then when EntityA.id=1 is populated with the sample data, its "mapList" property will have 1 entry in the map (key="foo"), pointing to a list of EntityB objects (with 2 entries).
Code:
EntityA entityA = // fetch where id=1;
System.out.println(entityA.size()); // prints: 1
List<EntityB> entityBList = entityA.get("foo");
// entityBList has 2 entries in it
for (EntityB entityB : entityBList) {
System.out.println(entityB.getId());
}
// loop prints:
2
3
So the big question is... is this possible without getting overly complicated with additional entity objects, etc? Any insight would be greatly appreciated!