-->
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.  [ 3 posts ] 
Author Message
 Post subject: multiple lists of same class
PostPosted: Sun Nov 13, 2005 2:42 pm 
Newbie

Joined: Wed Jul 06, 2005 1:56 am
Posts: 3
Hibernate version: 3.0.4

I have a class that contains multiple lists each containing the same class. For example:

class Wiget {
... other fields ...
List<String> innerColors;
List<String> outerColors;
}

Clearly, Hibernate needs some way to determine which strings go into which list.

The lists map to a Colors table:

CREATE TABLE colors (
wiget_id INT NOT NULL,
color VARCHAR(30) NOT NULL,
color_group VARCHAR(30) NOT NULL,
position INT NOT NULL,
PRIMARY KEY(wiget_id, color_group))

wigit_id is the foreign key to the Wiget object (Wiget table);
color is the color string;
color_group is the discriminator as to which list the color should go into;
position is the index in the list for the color string.

What I can't figure out is how to structure the mapping.

I tried the following:

<list name="innerColors" table="colors">
<key>
<column name="wiget_id"/>
<column name="color_group" check="'inner'"/>
</key>
<index column="position"/>
<element type="string" column="color" not-null="true"/>
</list>

Hibernate complains that the key for the list is not a valid foreign key to the Wigets table (the primary key of the Wigits table is just a wiget_id).

If I can only have the wiget_id in the <key> field, then how do I define the mapping so Hibernate knows to use the color_group column as a discriminator? I tried a "where" attribute in <list>, but that only helps with retrieval and not with saves. (I suspect the "check" attribute in my mapping above has the same limitation.)

Seems there must be a way to tell Hibernate to automatically insert the value 'inner' into color_group when saving and use the value 'inner' to discriminate on retrieval.

I can modify the table structure if that would help.

Has anyone solved this problem already?

It seems to me that having multiple lists or sets containing the same class of objects would be a common situation (not just limited to my example of String).


Top
 Profile  
 
 Post subject: try this:
PostPosted: Mon Nov 14, 2005 9:08 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
<list name="innerColors" table="colors" where="color_group='inner'">
<key>
<column name="wiget_id"/>
</key>
<index column="position"/>
<element type="string" column="color" not-null="true"/>
</list>

http://www.hibernate.org/hib_docs/v3/re ... ns-mapping

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 6:17 am 
Newbie

Joined: Thu May 17, 2007 5:29 am
Posts: 13
Hi,
I'm having the same problem ... can someone help.
Like ppanec said "where"works with retreival only, I need something like a discriminator which can be automatically inserted/checked through mapping.

_________________
--
Saugata
Please rate if this post helps


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