我想为以下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
}
我的表格是这些:
CREATE TABLE topics (
topicid int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (topicid)
)
CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)
CREATE TABLE newstopics (
topicid int(11) NOT NULL,
newsid int(11) NOT NULL,
PRIMARY KEY (topicid,newsid),
CONSTRAINT FOREIGN KEY (topicid) REFERENCES topics (topicid) ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY (newsid) REFERENCES news (id) ON DELETE CASCADE
)
我正在尝试这样的事情,但它不起作用:
<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>
目标是实现双向映射,这意味着当一个映射中的某些内容发生更改时,newstopics的内容也会更新。我已经尝试了多种变体来映射地图,但没有运气。最令人不安的方面是hibernate从/向newstopics表生成的SELECT和INSERT正在寻找更多的2列。例如INSERT INTO newstopics(topicid,newsid,id)VALUES? ? ?