-->
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.  [ 4 posts ] 
Author Message
 Post subject: mapping java Collections map with a List as value type
PostPosted: Fri Jan 27, 2006 7:05 pm 
Newbie

Joined: Fri Jan 27, 2006 6:55 pm
Posts: 2
Hibernate version:
3
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="src.Person" table="PERSONS">
       <id name="id" type="long" column="uid">
           <generator class="increment"/>
       </id>
      
       <property name="name" column="names" type="string"/>
      
       <joined-subclass name="src.Student" table="STUDENTS">
           <key column="uid"/>
           <property name="grade" column="grade" type="integer"/>
           <property name="sessionCount" column="sessions" type="integer"/>
           <property name="level" column="boardLevel" type="integer"/>
           <property name="position" column="boardPosistion" type="integer"/>
           <map name="scores">
              <key column="id"/>
              <index column="gradeID" type="integer"/>
              <element column="score" type="double"/>
           </map>       
       </joined-subclass>
        
       <joined-subclass name="src.Teacher" table="TEACHERS">
          <key column="uid"/>
          <property name="password" column="password" type="string"/>
       </joined-subclass>
      
       <joined-subclass name="src.BoardMember" table="BOARDMEMBERS">
          <key column="uid"/>
          <property name="password" column="password" type="string"/>
       </joined-subclass>
   </class>
   
</hibernate-mapping>


Hello. I have an inheritence model that has a "Person" as the Parent class and "Student"/"Teacher"/"BoardMember" as the sub-classes.

The student class contains an instance variable called "scores", which is a Map<Integer,List<Double>>. I cant seem to figure out how to setup the mapping file to accomodate the list of doubles as an element of the map.

Ive been reading about many-to-many and one-to-many associations but cant seem to wrap my head around how it would be useful in this situation.

If anyone could tell me how this mapping would be possible I'd appreciate it.

Thanks,
Andrew


Top
 Profile  
 
 Post subject: Mapping a MultiMap
PostPosted: Fri Jan 27, 2006 8:07 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
A Map<Integer,List<Double>> is not a simple Map - it's a MultiMap. I'm nearly certain that Hibernate does not support MultiMaps directly.

One way to deal with this problem is to use composite map keys (see section 8.3 of the documentation). You'll need to build a new key class to use instead of Integer. That class will contain two integers - one is the original key, the other is the sequence number of the grade in the list of grades. So instead of this Map

100->{3.5, 3.6, 4.0 }
101->{2.7, 3.0, 2.9 }

you'll build this:

(100,0) -> 3.5
(100,1) -> 3.6
(100,2) -> 4.0
(101,0) -> 2.7
(101,1) -> 3.0
(101,2) -> 2.9

You can put the same Java interface on top of this new Map, and make the new composite key package-private to hide the details completely. You'll have to write a bit more code too, but I'm certain that all programmers enjoy writing code, so the more - the better :-)

Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 8:51 pm 
Newbie

Joined: Fri Jan 27, 2006 6:55 pm
Posts: 2
So there's no way that I could have the element of the map be.... say an integer? And then use that integer collumn as the key of a table join, where that table contains the double values?

I also don't completely follow your solution... but maybe it'll make more sense when I get some time to read the 8.3 documentation.

Alternatively does anyone know of another solution for storing the same data?

Essentially all I need is a list of doubles corresponding to 5 different gameIDs. I suppose I could just store 5 lists in the student class or a List of Lists?


Top
 Profile  
 
 Post subject: Re: mapping java Collections map with a List as value type
PostPosted: Tue Nov 10, 2009 4:05 am 
Newbie

Joined: Mon Oct 12, 2009 6:07 am
Posts: 7
Even I have the same issue..any help wi=ould be appreciated


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

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.