-->
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: unidirectional collection mapping
PostPosted: Wed Apr 09, 2008 6:40 am 
Newbie

Joined: Thu Jun 07, 2007 10:23 am
Posts: 9
Hi,

I would like to define Collection mapping and can not find the right solution.

Source is an object called the Category that owns few Labels, where a Label only belongs to one Category. A Label always has a language (en, de, etc.). And finally per Category the Label is clearly identified by the its language.

Category:
Code:
Map<String, Label> getLabels() Map <String, Label> getLabels ()

returns a Map of all the Labels with the Label language as key of the Map.
Label has "lang", "text" and "description" as fields with corresponding getters and setters.

Every mapping I tried so far did not produce a proper DDL as it should logically look like:

Code:
create table Labels (category integer not null, lang varchar(255), text varchar(255), description varchar(255), primary key (category, lang))

with "category" being the foreign key to Category.

Instead "category, long, text, description" are used as the primary key, which causes to big keys in MySQL and DB2 does not work, because parts of the key can be NULL
Code:
<map name="labels" cascade="all" table="Labels">
  <key>
     <column name="category"/>
  </key>
  <map-key type="string" formula="lang"/>
  <composite-element class="Label">
    <property name="lang" not-null="true"/>
    <property name="text" />
    <property name="description" not-null="false"/>
  </composite-element>
</map>

I also have tried a one-to-many mapping of Label, as described in the section "6.2.5. One-to-many associations". This does not work because Label is a weak entity, that without the accompanying "strong" entity can not exist. I can not define a primary key for Label.

How should such a mapping look like so that "category, lang" is the primary key?

Thanks in advance.

Thomas


Top
 Profile  
 
 Post subject: Solution: unidirectional collection mapping
PostPosted: Mon Apr 14, 2008 9:08 am 
Newbie

Joined: Thu Jun 07, 2007 10:23 am
Posts: 9
After a huge step by step debug session I found the solution in IndexedCollection.createPrimaryKey()

Code:
         if (isFormula) {
            //if it is a formula index, use the element columns in the PK
            pk.addColumns( getElement().getColumnIterator() );
         }
         else {
            pk.addColumns( getIndex().getColumnIterator() );
         }


if isFormular==true then all columns will be used as a key. So I had to get it false so that just the map-key will be a part of the primary key.

Code:
<map name="labels" cascade="all" table="Labels">
  <key>
     <column name="category"/>
  </key>
  <map-key type="string" column="lang"/>
  <composite-element class="Label">
    <property name="lang" formula="lang"/>
    <property name="text" />
    <property name="description" not-null="false"/>
  </composite-element>
</map>


So I'm using now the formula for the property of Class label.


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.