开发者

bidirectional many-to-many association using a Map instead of a Set

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

<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>开发者_如何学Go

    <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>

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.

<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>

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 link to 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


Bidirectional Association doesn't work using LIST & MAP. It works fine using SET only. Using LIST it will generate unexpected values of index orders while persisting from both the sides and you got the error with MAP eventually.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜