Java Hibernate创建地图集合映射

Java Hibernate创建地图集合映射,java,hibernate,Java,Hibernate,我想为以下POJO类创建Hibernate XML映射: public class Topics { private int topicId; private Map<Integer,News> news; //setters and getters } public class News { private int newsId; private Map<Integer,Topics> newstopi

我想为以下POJO类创建Hibernate XML映射:

public class Topics {

      private int topicId;

      private Map<Integer,News> news;

      //setters and getters
}

public class News {

     private int newsId;

     private Map<Integer,Topics> newstopics;

     //setters and getters
}
我正在尝试类似的方法,但不起作用:

<class name="Topics" table="topics">
   <id name="topicId" type="int" column="topicid"><generator class="native"/></id>
   <map name="news" table="newstopics">
        <key column="topicid"/>
        <map-key  column="newsid" type="int"/>
        <many-to-many column="newsId" class="News" />
    </map>
</class>

 <class name="News" table="news">
    <id name="newsId" type="int" column="id"><generator class="native"/></id>
    <map name="newstopics" table="newstopics">
        <key column="newsid" />
        <map-key  column="topicid" type="int"/>
         <many-to-many column="topicId" class="Topics" />
    </map>
</class>


目标是实现双向映射,这意味着当一个映射中的内容发生变化时,新闻主题的内容也会得到更新。我尝试了多种不同的地图绘制方法,但没有成功。hibernate从newstopics表中选择并插入到newstopics表中的最令人不安的方面是查找超过2列的内容。例如,在新闻主题(topicid、newsid、id)中插入值

为什么要使用
地图
?典型的做法是使用一对多映射,例如
主题
列表
,其中
主题
包含
主题
-
id
的主键。数据模型也很混乱-新闻包含主题-好的,新闻是主题的容器。但是主题也包含新闻?我使用地图来访问和更新对象,而不必浏览整个集合。这个数据模型只是一个例子,我的应用程序中还有其他类似的关系。而且,一个主题应该包含新闻,这样你就可以按主题列出新闻
<class name="Topics" table="topics">
   <id name="topicId" type="int" column="topicid"><generator class="native"/></id>
   <map name="news" table="newstopics">
        <key column="topicid"/>
        <map-key  column="newsid" type="int"/>
        <many-to-many column="newsId" class="News" />
    </map>
</class>

 <class name="News" table="news">
    <id name="newsId" type="int" column="id"><generator class="native"/></id>
    <map name="newstopics" table="newstopics">
        <key column="newsid" />
        <map-key  column="topicid" type="int"/>
         <many-to-many column="topicId" class="Topics" />
    </map>
</class>