-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Map<String, List<EntityB>>... is it possible to
PostPosted: Wed Aug 22, 2007 2:48 pm 
Newbie

Joined: Wed Aug 22, 2007 1:57 pm
Posts: 1
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.