-->
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: How to map a Map of Lists of entities?
PostPosted: Thu Jun 08, 2006 9:52 pm 
Newbie

Joined: Fri Dec 16, 2005 2:50 pm
Posts: 2
Hi,

I'm having a little difficulty figuring out how to map nested collections of entities. In particular, I am trying to map a Map of Lists of persistent entities.

The example Java declaration is as follows:

Code:
private Map<String, List<Entity>> map = new HashMap<String, List<Entity>>();


The mapping is then as follows:

Code:
<map name="map" table="map_of_lists">
  <key column="entity_id" />
  <index column="map_key" type="java.lang.String" />
  <???>
</map>


The "thing to be mapped" (i.e., the value in the java.util.Map) is neither directly my Entity class, nor is it a component or a composite-element. Rather, it is a collection of entities.

Note: I am not asking how to map my Entity class: the mapping for the Entity works fine.

I would be very greatful for tips on what I need to insert for the ??? section to define a proper mapping for a nested collection of entities.

Thanks in advance for any insight you might be able to give me!

Regards,

Sam

p.s. enviroment details follow.

Hibernate version: 3.1.3

Name and version of the database you are using: mysql-5.0.15-win32


Last edited by sbrannen on Fri Jun 09, 2006 6:39 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 10:56 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There is no direct support for that. The only solution that I can think of that doesn't require you to change your java code is to put a <component> in your map, and the list in the component.

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


Top
 Profile  
 
 Post subject: Maps do not support component sub-elements
PostPosted: Fri Jun 09, 2006 7:35 am 
Newbie

Joined: Fri Dec 16, 2005 2:50 pm
Posts: 2
Hi tenwit,

Thanks for the quick reply.

tenwit wrote:
There is no direct support for that.


I somewhat assumed and feared that. :(

tenwit wrote:
The only solution that I can think of that doesn't require you to change your java code is to put a <component> in your map, and the list in the component.


I also thought that sounded feasible, but looking at the DTD below, you can see that <component> is not a supported sub-element of <map>.

http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd

Here are the supported sub-elements:

(element|one-to-many|many-to-many|composite-element|many-to-any)

If you read further, you will notice the following information for the composite-element, with emphasis on "Composite elements may not hold nested collections":

Quote:
A composite element allows a collection to hold instances of an arbitrary
class, without the requirement of joining to an entity table. Composite elements
have component semantics - no shared references and ad hoc null value semantics.
Composite elements may not hold nested collections.


So, it does not appear that I can achieve a mapping of nested collections. Instead, I assume I will actually have to modify my Java code.

Do you see any other way around this issue? Am I missing something?

Thanks,

Sam

_________________
Sam Brannen


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 11, 2006 7:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Looks like you need to use an entity in the <map>, and the entity would have just an <id>, optionally a <many-to-one> back to your existing (parent) entity, and the <list> that you need.

I tried figuring out what the DB schema that implements your initial idea would look like, and I don't think that it's possible. You need a table to hold the list, and that table needs the id of the parent entity, the list index, and whatever data you want to store. And probably a surrogate key, for convenience. All of that means that you need an entity, so it looks like the hibernate restrction that you can't put a collection in a colleciton isn't because hibernate can't handle it, but rather that RDBMSs can't handle it.

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


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.