-->
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.  [ 6 posts ] 
Author Message
 Post subject: a Map mapping question.
PostPosted: Thu Jul 06, 2006 2:53 am 
Newbie

Joined: Wed Jul 05, 2006 10:37 pm
Posts: 6
How would you go about mapping this Map variable:

Code:
Map<String, <List<String>> map


here's roughly what i have in my mapping file atm:

Code:
<map name="requestHeaders" access="field">
          <key column="requestHeaders"/>
          <map-key column="keys" type="string"/>
          <element column="values" type="java.util.List"/>
</map>


Which of course doesn't want to work. Any suguestions?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have to use an intermediate entity, you cannot do it directly. That is, your POJO will contain a Map<String, CustomListOfString>, where CustomListOfStrings has its own DB identifier and a list of strings.

This isn't an hibernate restriction, it's a relational DB restriction. That shape cannot be described in SQL without the extra ID column that CustomListOfStrings provides.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:33 pm 
Newbie

Joined: Wed Jul 05, 2006 10:37 pm
Posts: 6
yeah i was starting to think along those lines. I stoped cursing hibernate after i learnt that JDO didn't really do that kind of thing either.

I understand the relational restriction, though it would be nice as this is a common structure for hibernate to create a psuedo table to make this work. Though then it would create one hellova mapping problem if you imagine: Map<String, <List <List String>>> m.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:39 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
I am doing something similar to this, and the underlying DB implementation is just a single String. I do a custom mapping in the Java VO load which basically splits the string on get and joins the collection on set. Then it's just a String-String mapping at the DB level. I can see why this wont work in more complex cases, but maybe for what you are doing it will...

dan

_________________
_________________
dan

If what I say is helpful, please rate the post as such by clicking 'Y'. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 11:04 pm 
Newbie

Joined: Wed Jul 05, 2006 10:37 pm
Posts: 6
aaaah nice one :)

but alas i loath filling my code with getters and setters lol.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 11:12 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
Here's some code to get you started along that treachorous path... =)

Code:
   public void setMetricsMap(Map<String, String[]> deepMap) {
      Map<String,String> map = new HashMap<String,String>();
      Iterator<String> it = deepMap.keySet().iterator();
      String key;
      while(it.hasNext()) {
         key = it.next();
         map.put(key,StringUtils.join(deepMap.get(key), '|'));
      }
      this.setFlatMetricsMap(map);
   }

   public Map<String, String[]> getMetricsMap() {
      Map<String,String> flatMap = this.getFlatMetricsMap();
      Map<String,String[]> map = new HashMap<String, String[]>();
      Iterator<String> it = flatMap.keySet().iterator();
      String key;
      while(it.hasNext()) {
         key = it.next();
         map.put(key,StringUtils.split(flatMap.get(key), '|'));
      }
      return map;
   }


Then map the field flatMetricsMap in your Hibernate code and call the metricsMap field in your Java code.

Have fun!


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