-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping a map for many-to-many association
PostPosted: Fri Oct 22, 2010 8:12 pm 
Newbie

Joined: Mon Oct 18, 2010 9:35 am
Posts: 10
Hi,

I am interested in how I can create a map like the following:
Code:
private Map<Integer, String> sizeIdNameMap = new HashMap<Integer, String>();
where the integer belongs to the sizeId and the String is the name from the following Classes and DB design:
Code:
public class Product implements Comparable<Product>, Serializable {
       private long id;
       private long otherProductData;
       private Map<Integer, String> sizeIdNameMap = new HashMap<Integer, String>();
       //constructor;setters;getters;equals;hashCode
}
public class Size implements Comparable<Size>, Serializable {
       private long id;
       private long name;
       //constructor;setters;getters;equals;hashCode
}

<hibernate-mapping package="beans">
   <class name="Size" table="Size">
      <id name="id" column="id">
         <generator class="native" />
      </id>
      <property name="name" type="string" column="name" />
   </class>
   <class name="Product" table="Product">
      <id name="id" column="id">
         <generator class="native" />
      </id>
      <property name="otherProductData" type="string" not-null="true" length="200"/>
      <map name="sizeIdNameMap" table="ProductSizeJunction">
         <key column="personId"/>
         <map-key column="sizeId" type="integer" />
         <many-to-many ???????>
            ???????
         </many-to-many>
      </map>
   </class>
</hibernate-mapping>

The database design:
Image
As you can see the many-to-many map mapping is missing. When I went through the reference, the examples with many to many association (and join/link tables) were limited to set mapping or mapping a map where the key and value from the map are columns of the join table, but I couldn't find an example where you can map columns from the table on the other side of the many to many association. As you can see, I need the value of the map to be the name column in the Size table. Unfortunately most of the books regarding Hibernate follow the reference examples (regarding map mapping with join tables).

If someone knows this, I would appreciate the help.

Kind Regards,
Despot


Top
 Profile  
 
 Post subject: Re: Mapping a map for many-to-many association
PostPosted: Sat Oct 23, 2010 12:06 pm 
Newbie

Joined: Mon Oct 18, 2010 9:35 am
Posts: 10
Hi again,

Regarding the same problem, I was thinking what else could be done if Hibernate prevents me to do this kind of mapping. I came up with this solutions:
1) Creating my own SizeNamePropertyMap that extends HashMap, and override the put(key, value) method so when Hibernate inputs the Size as a value, I get the name property of the Size object and input that property as the value of the map. See this solution in more details below.
2) I keep my map in the form Map<sizeId, Size> instead of Map<sizeId, name> => this will require changes in the rest of the application.
3) Existing hibernate mapping solution for this kind of problem (which I don't know). If you know this one, provide it in a reply, please.
So what do you think? What is the best solution here?


The details of the 1) solution:
Code:
public class SizeNamePropertyMap extends HashMap<Object, Object> {
   @Override
   public Object put(Object key, Object value) {
      String finalValue;
      if (value instanceof String){
         finalValue = (String) value;
      } else if (value instanceof Size){
         finalValue = ((Size) value).getName();
      } else {
         throw new RuntimeException("Class cast exception. The value is instance of:" +
               value.getClass().getName() + ". Should be either String or Size.");
      }
      return super.put(key, finalValue);
   }
}
public class Product implements Comparable<Product>, Serializable {
       private long id;
       private long otherProductData;
       private Map<Integer, Object> sizeIdNameMap = new SizeNamePropertyMap<Integer, String>();
       //constructor;setters;getters;equals;hashCode
}

hibernate mapping for the map element:
      <map name="sizeIdNameMap" table="ProductSizeJunction">
         <key column="personId"/>
         <map-key column="sizeId" type="integer" />
         <composite-element class="Size" >
            <property name="id" column="id" />
            <property name="name" column="name" />
         </composite-element>
      </map>


Regards,
Despot


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.