-->
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: bidirectional many-to-many assoc. using a Map instead of Set
PostPosted: Thu Jun 30, 2011 4:18 pm 
Newbie

Joined: Mon Apr 04, 2011 3:41 pm
Posts: 12
I'm having a bear of a time with this and any suggestions would be greatly appreciated.

I have a bi-directional many-to-many association mapping between two entities (FlashCard and Tag) that is currently successfully represented as a java.util.Set. I'd like to change the collection type to a java.util.Map.

Here's an excerpt of the working mappings using Set
Code:
<hibernate-mapping>

   <class name="FlashCard" table="FLASHCARD">

      <id name="flashCardId" type="int" column="FLASHCARD_ID">
         <meta attribute="scope-set">public</meta>
         <generator class="native" />
      </id>

      <property name="question" type="string">
         <meta attribute="use-in-tostring">true</meta>
         <column name="QUESTION" not-null="true" unique="true" />
      </property>

      <set name="tags" table="FLASHCARD_TAG">
         <meta attribute="field-description">Tags for this FlashCard</meta>
         <key column="FLASHCARD_ID" />
         <many-to-many class="Tag"
            column="TAG_ID" />
      </set>

   </class>

   <class name="Tag" table="TAG">

      <id name="tagId" type="int" column="TAG_ID">
         <meta attribute="scope-set">public</meta>
         <generator class="native" />
      </id>

      <property name="name" type="string">
         <meta attribute="use-in-tostring">true</meta>
         <column name="NAME" not-null="true" unique="true" />
      </property>

      <set name="flashcards" table="FLASHCARD_TAG" inverse="true">
         <meta attribute="field-description">FlashCards for this Tag</meta>
         <key column="TAG_ID" />
         <many-to-many class="FlashCard"
            column="FLASHCARD_ID" />
      </set>

   </class>

</hibernate-mapping>


Ok, so like I said this is working but I'd like to change the collection to type java.util.Map.

I played with the mappings quite a bit and was finally able to get the following to partially work.

Code:
<hibernate-mapping>

   <class name="FlashCard" table="FLASHCARD">

      <id name="flashCardId" type="int" column="FLASHCARD_ID">
         <meta attribute="scope-set">public</meta>
         <generator class="native" />
      </id>

      <property name="question" type="string">
         <meta attribute="use-in-tostring">true</meta>
         <column name="QUESTION" not-null="true" unique="true" />
      </property>

      <map name="tags" cascade="all" table="FLASHCARD_TAG">
         <meta attribute="field-description">Tags for this FlashCard</meta>
         <key column="FLASHCARD_ID" not-null="true" />
         <map-key type="String" column="NAME" formula="NAME"/>
         <many-to-many column="TAG_ID" class="Tag"/>
      </map>

   </class>

   <class name="Tag" table="TAG">

      <id name="tagId" type="int" column="TAG_ID">
         <meta attribute="scope-set">public</meta>
         <generator class="native" />
      </id>

      <property name="name" type="string">
         <meta attribute="use-in-tostring">true</meta>
         <column name="NAME" not-null="true" unique="true" />
      </property>

      <map name="flashcards" inverse="true" cascade="all" table="FLASHCARD_TAG">
         <meta attribute="field-description">FlashCards for this Tag</meta>
         <key column="TAG_ID" not-null="true" />
         <map-key type="String" column="QUESTION" formula="QUESTION" />
         <many-to-many column="FLASHCARD_ID" class="FlashCard"/>
      </map>

   </class>

</hibernate-mapping>


When I say these mappings "partially work" I mean that I was able use these mapping to generate the database tables and the entity classes. The entity classes did indeed include a collection of type java.util.Map. This all looked find and compiles without errors.

However, when I run the code I get runtime errors from hibernate as follows:
org.hibernate.MappingException: Could not determine type for: String, at table: FLASHCARD_TAG, for columns: [org.hibernate.mapping.Formula( NAME )]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)

The Hibernate documentation shows an example of a bi-directional many-to-many association but it uses a Set. Here's a linkto the docs.

Summary

- There are two entities: FlashCard and Tag
- The FlashCard entity has a collection of references to other entities of type Tag
- The Tag entity has a collection of references to other entities of type FlashCard
- This is a many-to-many, bi-directional association
- FLASHCARD_TAG is the association table
- The association is currently represented as a java.util.Set which according to the Hibernate documentation is the most commonly used collection mapping.
- I want to use java.util.Map instead of the java.util.Set


Top
 Profile  
 
 Post subject: Re: bidirectional many-to-many assoc. using a Map instead of Set
PostPosted: Fri Jul 01, 2011 12:10 pm 
Newbie

Joined: Mon Apr 04, 2011 3:41 pm
Posts: 12
anyone have any ideas on this one? any input is greatly appreciated


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.