我对此很感兴趣,任何建议都将不胜感激。
我在两个实体(FlashCard和Tag)之间有一个双向的多到多关联映射,目前,成功地将表示为一个。我想将集合类型的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:无法确定类型for: String,at table: FLASHCARD_TAG,for columns: org.hibernate.mapping.Formula( NAME )
Hibernate文档展示了一个双向多到多关联的例子,但是它使用了一个集合。这里是给文档的链接。
摘要
发布于 2017-11-01 12:41:24
双向关联不使用LIST和MAP。它的工作原理很好,只使用SET。使用LIST,它将在从两边持久化的同时生成索引顺序的意外值,最终您将得到MAP的错误。
https://stackoverflow.com/questions/6539209
复制相似问题