-->
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: Mapping a Map with key and value from different tables
PostPosted: Wed Jun 13, 2007 11:14 am 
Newbie

Joined: Thu Jan 11, 2007 11:24 am
Posts: 7
I have a one-to-many relationship between a Brand and BrandItem. There is also a one-to-many relationship between an Item and BrandItem.

I want my Brand object to contain a Map of Item name (from the Item table) to BrandItem value (from the BrandItem table) - i.e. the key and value for the Map come from different tables.

Is this possible in Hibernate (without creating a view and creating the Map from this)?

Thanks


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jun 13, 2007 5:25 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
This will get you a map of item and brandItem inside brand class.

Let me know if this will do the job.

Code:
<class name="Brand">
     <id name="id">
        <generator class="increment"/>
     </id>
     <map name="brandItems" table="brandItem">
        <key column="id"></key>
        <index-many-to-many class="Item" column="item" />
        <one-to-many class="BrandItem" />
     </map>
     <property name="name"/>
  </class>
 
  <class name="Item">
     <id name="id">
        <generator class="increment"/>
     </id>
     <set name="brandItems">
        <key column="item"/>
        <one-to-many class="BrandItem" />
     </set>
     <property name="name"/>
  </class>
 
  <class name="BrandItem">
     <id name="id">
        <generator class="increment"/>
     </id>
   <many-to-one name="brand" class="Brand" />
   <many-to-one name="item" class="Item" />
     
  </class>


Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 4:29 am 
Newbie

Joined: Thu Jan 11, 2007 11:24 am
Posts: 7
Thanks - that does help.

What this gives me is a Map, keyed on the stringifed Item class.

What I really want is it to be keyed on the value of a property from the Item class. I can achieve this by overriding toString() in the Item class, to give the value of the property I want; this isn't ideal.

Also, it performs two selects - one from BrandItem, and one from Item. I would prefer it to do a join.

Do you know if there is any way I can get round this two issues?

Thanks a lot

Jonny


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 6:49 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Not sure what you mean by "stringified".

If you call brandItems.keySet() it will return you a set of Items. then you can do getName from the Item.

Quote:
Also, it performs two selects - one from BrandItem, and one from Item. I would prefer it to do a join.


Well hibernate is a very powerful tool, optimizations are possible at many levels, what I have provided is a simple solution that should work. Feel free to explore and find possible optimizations.

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 7:21 am 
Newbie

Joined: Thu Jan 11, 2007 11:24 am
Posts: 7
By stringified, I meant the String representation of the object, as given by its toString() method.

Something I didn't mention is that I'm using 2nd level caching for the Map. I want to be able to access items keyed on the Item name, very frequently. I don't want to have to deal with a KeySet each time.

You mention optimisations, but do you know whether it's possible to configure Hibernate to put data into a Map using a join?

Thanks


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 8:41 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
To optimize, there are attributes on map and index like formula, but I am not sure how to use them, you can experiment a bit and find out.

But to put it simply you can use toString approach, though I don't like it because the names are not unique and hence can lead to issues in future.

Regards,
Jitendra


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.