使用Map而不是Set的双向多对多关联

时间:2011-06-30 18:11:58

标签: hibernate

我对此有一段时间,任何建议都会非常感激。

我在两个实体(FlashCard和Tag)之间有一个双向的多对多关联映射,当前成功表示为 java.util.Set 。我想集合类型更改为 java.util.Map

这是使用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>

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

好的,就像我说的那样有效,但我想将集合更改为java.util.Map类型。

我玩了很多映射,最后能够得到以下部分工作。

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

当我说这些映射“部分工作”时,我的意思是我能够使用这些映射来生成数据库表和实体类。实体类确实包含java.util.Map类型的集合。这一切都看起来和编译没有错误。

然而,当我运行代码时,我从hibernate获得运行时错误,如下所示: org.hibernate.MappingException:无法确定类型:String,在表:FLASHCARD_TAG,对于列:[org.hibernate.mapping.Formula(NAME)]     at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)

Hibernate文档显示了双向多对多关联的示例,但它使用了Set。这是文档的link

摘要

  • 有两个实体:FlashCard和Tag
  • FlashCard实体具有对Tag
  • 类型的其他实体的引用集合
  • Tag实体具有对FlashCard类型的其他实体的引用集合
  • 这是一个多对多的双向关联
  • FLASHCARD_TAG是关联表
  • 该关联目前表示为java.util.Set,根据Hibernate文档,它是最常用的集合映射。
  • 我想使用java.util.Map而不是java.util.Set

1 个答案:

答案 0 :(得分:0)

双向关联无法使用LIST&amp; MAP。它只能使用SET正常工作。使用LIST,它会生成意外的索引顺序值,同时从两边持续存在,最终会出现MAP错误。